Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/diagnostics/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

// Not in scope: check if the name refers to a trait importable from elsewhere.
if macro_kind == MacroKind::Derive {
// An exact derive macro candidate is much more likely to be intended than any
// same-named traits, so we only consider them if there are no import suggestions for the derive macro.
if macro_kind == MacroKind::Derive && import_suggestions.is_empty() {
let trait_candidates =
self.lookup_import_candidates(ident, TypeNS, parent_scope, |res| {
matches!(res, Res::Def(DefKind::Trait, _))
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/macros/auxiliary/derive-serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(Serialize)]
pub fn derive_serialize(_: TokenStream) -> TokenStream {
TokenStream::new()
}
23 changes: 23 additions & 0 deletions tests/ui/macros/derive-import-hides-trait-issue-162260.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Regression test for https://github.com/rust-lang/rust/issues/162260.
//! An available derive import should take precedence over same-named traits.

//@ run-rustfix
//@ proc-macro: derive-serialize.rs
//@ edition: 2024

#![allow(dead_code)]

use derive_serialize::Serialize;

extern crate derive_serialize;

mod traits {
pub trait Serialize {}
}

#[derive(Serialize)]
//~^ ERROR cannot find derive macro `Serialize` in this scope
//~| ERROR cannot find derive macro `Serialize` in this scope
struct S;

fn main() {}
21 changes: 21 additions & 0 deletions tests/ui/macros/derive-import-hides-trait-issue-162260.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Regression test for https://github.com/rust-lang/rust/issues/162260.
//! An available derive import should take precedence over same-named traits.

//@ run-rustfix
//@ proc-macro: derive-serialize.rs
//@ edition: 2024

#![allow(dead_code)]

extern crate derive_serialize;

mod traits {
pub trait Serialize {}
}

#[derive(Serialize)]
//~^ ERROR cannot find derive macro `Serialize` in this scope
//~| ERROR cannot find derive macro `Serialize` in this scope
struct S;

fn main() {}
25 changes: 25 additions & 0 deletions tests/ui/macros/derive-import-hides-trait-issue-162260.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: cannot find derive macro `Serialize` in this scope
--> $DIR/derive-import-hides-trait-issue-162260.rs:16:10
|
LL | #[derive(Serialize)]
| ^^^^^^^^^
|
help: consider importing this derive macro
|
LL + use derive_serialize::Serialize;
|

error: cannot find derive macro `Serialize` in this scope
--> $DIR/derive-import-hides-trait-issue-162260.rs:16:10
|
LL | #[derive(Serialize)]
| ^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider importing this derive macro
|
LL + use derive_serialize::Serialize;
|

error: aborting due to 2 previous errors

2 changes: 0 additions & 2 deletions tests/ui/macros/issue-88228.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ mod hey { //~ HELP consider importing this derive macro

#[derive(Bla)]
//~^ ERROR cannot find derive macro `Bla`
//~| NOTE `Bla` is a trait, not a derive macro
//~| HELP consider implementing `Bla` for your type manually
struct A;

#[derive(println)]
Expand Down
7 changes: 2 additions & 5 deletions tests/ui/macros/issue-88228.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: cannot find macro `bla` in this scope
--> $DIR/issue-88228.rs:22:5
--> $DIR/issue-88228.rs:20:5
|
LL | bla!();
| ^^^
Expand All @@ -10,7 +10,7 @@ LL + use crate::hey::bla;
|

error: cannot find derive macro `println` in this scope
--> $DIR/issue-88228.rs:16:10
--> $DIR/issue-88228.rs:14:10
|
LL | #[derive(println)]
| ^^^^^^^
Expand All @@ -23,9 +23,6 @@ error: cannot find derive macro `Bla` in this scope
LL | #[derive(Bla)]
| ^^^
|
note: `Bla` is a trait, not a derive macro
--> $SRC_DIR/core/src/marker.rs:LL:COL
= help: consider implementing `Bla` for your type manually
help: consider importing this derive macro through its public re-export
|
LL + use crate::hey::Bla;
Expand Down
Loading