Skip to content

cfg resolve diagnostic doesn't show up for derive macro that matches trait name #134448

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
joshka opened this issue Dec 18, 2024 · 2 comments
Open
Labels
A-cfg Area: `cfg` conditional compilation A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshka
Copy link
Contributor

joshka commented Dec 18, 2024

Code

Without the macro feature enabled.

use axum::extract::FromRef;

#[derive(Clone, FromRef)]
struct AppState {
    db: SqlitePool,
}

Current output

error: cannot find derive macro `FromRef` in this scope
  --> src/main.rs:33:17
   |
33 | #[derive(Clone, FromRef)]
   |                 ^^^^^^^
   |
note: `FromRef` is imported here, but it is only a trait, without a derive macro
  --> src/main.rs:1:12
   |
1  | use axum::{extract::FromRef, Router};
   |            ^^^^^^^^^^^^^^^^

Desired output

error[E0432]: unresolved import `axum::extract::FromRef`
  --> src/main.rs:2:5
   |
2  | use axum::extract::FromRef;
   |     ^^^^^^^^^^^^^^^^^ no `FromRef` in `extract`
   |
note: found an item that was configured out
  --> /Users/joshka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.9/src/extract/mod.rs:xx:yy
   |
xx | macro_rules! FromRef;
   |         ^^
note: the item is gated behind the `macros` feature
  --> /Users/joshka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.9/src/extract/mod.rs:xx:yy

Rationale and extra context

The detection of cfg'd out items should treat items that match the expected type (e.g. a macro) as higher priority than items which match the name (e.g. FromRef)

Other cases

Rust Version

rustc --version --verbose
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1

Anything else?

Similar to but not the same as #132166
Possibly relevant to #129183

@joshka joshka added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2024
@fmease fmease added A-resolve Area: Name/path resolution done by `rustc_resolve` specifically A-cfg Area: `cfg` conditional compilation D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Dec 18, 2024
@Noratrieb
Copy link
Member

I think this is caused by an entirely different root cause and the duplicate name is a red herring, it shouldn't matter to resolve whether there's an item of the same name already in a different namespace.
I remember this working correctly for serde, which uses the same pattern.

@Noratrieb Noratrieb changed the title Detect cfg'd out items where they share a name with another item (e.g. macro + trait) cfg resolve diagnostic doesn't show up for axum macro Dec 18, 2024
@estebank
Copy link
Contributor

estebank commented Mar 9, 2025

As pointed out in the comment above, the same happens with serde and the default feature configuration:

Compiling serverless_chat v0.0.1 (/home/gwenthekween/Documents/personal/SCA)    
error: cannot find derive macro `Serialize` in this scope                          
 --> src/main.rs:3:10                                                              
  |                                                                                
3 | #[derive(Serialize, Deserialize, Debug)]                                       
  |          ^^^^^^^^^                                                             
  |                                                                                
note: `Serialize` is imported here, but it is only a trait, without a derive macro 
 --> src/main.rs:1:13                                                              
  |                                                                                
1 | use serde::{Serialize, Deserialize};                                           
  |             ^^^^^^^^^                                                          
                                                                                   
error: cannot find derive macro `Deserialize` in this scope                        
 --> src/main.rs:3:21                                                              
  |                                                                                
3 | #[derive(Serialize, Deserialize, Debug)]                                       
  |                     ^^^^^^^^^^^                                                
  |                                                                                
note: `Deserialize` is imported here, but it is only a trait, without a derive macr
 --> src/main.rs:1:24                                                              
  |                                                                                
1 | use serde::{Serialize, Deserialize};                                           
  |                        ^^^^^^^^^^^

reported at https://kitsunes.club/notes/a555uzxgvz

The compiler should absolutely mention that the derive is behind a feature flag and be explicit about the Cargo.toml entry that needs to be modified. Related, but not quite the same: #47608, #87717.

@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Mar 9, 2025
@joshka joshka changed the title cfg resolve diagnostic doesn't show up for axum macro cfg resolve diagnostic doesn't show up for derive macro that matches trait name Mar 9, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-cfg Area: `cfg` conditional compilation A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants