From 34a8d25e1d84b5494a369beb3628a97750cab7d9 Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Sun, 27 Sep 2026 15:46:38 +0200 Subject: [PATCH 1/2] raise ambiguity error on attribute macro that looks like tool attribute --- compiler/rustc_resolve/src/diagnostics/mod.rs | 11 +++++++++++ compiler/rustc_resolve/src/macros.rs | 10 ++++++++++ tests/ui/diagnostic_namespace/existing_proc_macros.rs | 2 +- .../diagnostic_namespace/existing_proc_macros.stderr | 11 +++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/ui/diagnostic_namespace/existing_proc_macros.stderr diff --git a/compiler/rustc_resolve/src/diagnostics/mod.rs b/compiler/rustc_resolve/src/diagnostics/mod.rs index 65ef919e9d0c7..01ec5bcb6cf29 100644 --- a/compiler/rustc_resolve/src/diagnostics/mod.rs +++ b/compiler/rustc_resolve/src/diagnostics/mod.rs @@ -1774,3 +1774,14 @@ pub(crate) enum UnusedImportsSugg { num_to_remove: usize, }, } + +#[derive(Diagnostic)] +#[diag("attribute macro `{$attr_path}` is ambiguous")] +#[note("ambiguous because of a name conflict with a tool namespace")] +#[note("`{$tool}` could refer to a tool namespace")] +pub(crate) struct ToolAttrAmbiguity { + #[primary_span] + pub path_span: Span, + pub attr_path: String, + pub tool: Symbol, +} diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 628b93bd5e46c..28633ea2ce39c 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -692,6 +692,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { }); } } + if kind == MacroKind::Attr + && let [tool, _second, ..] = path.segments.as_slice() + && self.registered_attr_tool_decls.contains_key(&IdentKey::new(tool.ident)) + { + self.dcx().emit_err(diagnostics::ToolAttrAmbiguity { + path_span: path.span, + attr_path: pprust::path_to_string(path), + tool: tool.ident.name, + }); + } } Res::NonMacroAttr(..) | Res::Err => {} _ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"), diff --git a/tests/ui/diagnostic_namespace/existing_proc_macros.rs b/tests/ui/diagnostic_namespace/existing_proc_macros.rs index 55b6b0ab441bc..0526c2aa86135 100644 --- a/tests/ui/diagnostic_namespace/existing_proc_macros.rs +++ b/tests/ui/diagnostic_namespace/existing_proc_macros.rs @@ -1,4 +1,3 @@ -//@ check-pass //@ proc-macro: proc-macro-helper.rs extern crate proc_macro_helper; @@ -17,6 +16,7 @@ mod test2 { } #[diagnostic::on_unimplemented] + //~^ ERROR attribute macro `diagnostic::on_unimplemented` is ambiguous trait Foo {} } diff --git a/tests/ui/diagnostic_namespace/existing_proc_macros.stderr b/tests/ui/diagnostic_namespace/existing_proc_macros.stderr new file mode 100644 index 0000000000000..2e8a6c7923a2c --- /dev/null +++ b/tests/ui/diagnostic_namespace/existing_proc_macros.stderr @@ -0,0 +1,11 @@ +error: attribute macro `diagnostic::on_unimplemented` is ambiguous + --> $DIR/existing_proc_macros.rs:18:7 + | +LL | #[diagnostic::on_unimplemented] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: ambiguous because of a name conflict with a tool namespace + = note: `diagnostic` could refer to a tool namespace + +error: aborting due to 1 previous error + From 168e53d7bb22833c777a3bedafdff1706cd461d3 Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Thu, 22 Oct 2026 00:14:23 +0200 Subject: [PATCH 2/2] add `no_implicit_prelude` test --- .../ui/tool-attributes/no_implicit_prelude.rs | 27 +++++++++++++++++++ .../no_implicit_prelude.stderr | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/ui/tool-attributes/no_implicit_prelude.rs create mode 100644 tests/ui/tool-attributes/no_implicit_prelude.stderr diff --git a/tests/ui/tool-attributes/no_implicit_prelude.rs b/tests/ui/tool-attributes/no_implicit_prelude.rs new file mode 100644 index 0000000000000..7b06ea30d73f6 --- /dev/null +++ b/tests/ui/tool-attributes/no_implicit_prelude.rs @@ -0,0 +1,27 @@ +#![no_implicit_prelude] +#![feature(register_tool)] +#![register_tool(my_tool)] + +mod test1 { + mod my_tool { + pub use ::std::derive as attr; + } + + #[my_tool::attr(Debug)] + //~^ ERROR attribute macro `my_tool::attr` is ambiguous + struct Foo; +} + +mod test2 { + mod clippy { + pub use ::std::derive as format_args; + } + + #[rustfmt::skip] + //~^ ERROR cannot find module or crate `rustfmt` in this scope + #[clippy::format_args(Debug)] + //~^ ERROR attribute macro `clippy::format_args` is ambiguous + struct Foo; +} + +fn main() {} diff --git a/tests/ui/tool-attributes/no_implicit_prelude.stderr b/tests/ui/tool-attributes/no_implicit_prelude.stderr new file mode 100644 index 0000000000000..64ef7891c42cb --- /dev/null +++ b/tests/ui/tool-attributes/no_implicit_prelude.stderr @@ -0,0 +1,27 @@ +error: attribute macro `my_tool::attr` is ambiguous + --> $DIR/no_implicit_prelude.rs:10:7 + | +LL | #[my_tool::attr(Debug)] + | ^^^^^^^^^^^^^ + | + = note: ambiguous because of a name conflict with a tool namespace + = note: `my_tool` could refer to a tool namespace + +error: attribute macro `clippy::format_args` is ambiguous + --> $DIR/no_implicit_prelude.rs:22:7 + | +LL | #[clippy::format_args(Debug)] + | ^^^^^^^^^^^^^^^^^^^ + | + = note: ambiguous because of a name conflict with a tool namespace + = note: `clippy` could refer to a tool namespace + +error[E0433]: cannot find module or crate `rustfmt` in this scope + --> $DIR/no_implicit_prelude.rs:20:7 + | +LL | #[rustfmt::skip] + | ^^^^^^^ use of unresolved module or unlinked crate `rustfmt` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0433`.