From 349718b31bcb9b7c57d9a102a25e6769bab2bf09 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 9 Nov 2026 12:17:50 -0500 Subject: [PATCH] Skip incoherent impl lookups for locally defined types --- compiler/rustc_middle/src/ty/trait_def.rs | 4 +++- .../run-make/crate-incoherent-impl-domain/local.rs | 9 +++++++++ .../run-make/crate-incoherent-impl-domain/rmake.rs | 14 +++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 16762b1440f..9887bf44d09 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -547,8 +547,10 @@ pub(super) fn trait_impls_by_const_arg_provider( /// Query provider for `incoherent_impls`. pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] { + // An inherent impl for a local definition is recorded by `inherent_impls`. + // Upstream crates cannot name this definition. if let Some(def_id) = simp.def() - && !find_attr!(tcx, def_id, RustcHasIncoherentInherentImpls) + && (def_id.is_local() || !find_attr!(tcx, def_id, RustcHasIncoherentInherentImpls)) { return &[]; } diff --git a/tests/run-make/crate-incoherent-impl-domain/local.rs b/tests/run-make/crate-incoherent-impl-domain/local.rs index 2493cf102d3..7a2b0434761 100644 --- a/tests/run-make/crate-incoherent-impl-domain/local.rs +++ b/tests/run-make/crate-incoherent-impl-domain/local.rs @@ -16,6 +16,15 @@ impl u16 { } } +struct Local; + +impl Local { + fn value(&self) -> u8 { + 17 + } +} + fn main() { + assert_eq!(Local.value(), 17); println!("{}", u16::local_domain_probe()); } diff --git a/tests/run-make/crate-incoherent-impl-domain/rmake.rs b/tests/run-make/crate-incoherent-impl-domain/rmake.rs index 65f3d0a0844..8767a548223 100644 --- a/tests/run-make/crate-incoherent-impl-domain/rmake.rs +++ b/tests/run-make/crate-incoherent-impl-domain/rmake.rs @@ -1,7 +1,7 @@ //@ ignore-cross-compile //@ needs-target-std -use run_make_support::{run, rust_lib_name, rustc}; +use run_make_support::{rfs, run, rust_lib_name, rustc}; fn main() { rustc().input("owner.rs").crate_type("rlib").edition("2024").run(); @@ -61,4 +61,16 @@ fn main() { local.run(); run("local").assert_stdout_equals(output); } + + // Looking up incoherent impls for a local type must not read its attributes. + rustc() + .input("local.rs") + .edition("2024") + .incremental("local-lookup") + .arg("-Zquery-dep-graph") + .arg("-Zdump-dep-graph") + .run(); + let edges = rfs::read_to_string("dep_graph.txt"); + assert!(edges.lines().any(|edge| edge.starts_with("typeck_root -> "))); + assert!(!edges.lines().any(|edge| edge == "incoherent_impls -> hir_attr_map")); }