Skip to content

Detect more cfgd out items in resolution errors #129183

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 2, 2025
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
3 changes: 1 addition & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,9 +1411,8 @@ impl InvocationCollectorNode for P<ast::Item> {
}
}
}

let mut idents = Vec::new();
collect_use_tree_leaves(ut, &mut idents);
collect_use_tree_leaves(&ut, &mut idents);
idents
} else {
self.kind.ident().into_iter().collect()
Expand Down
25 changes: 12 additions & 13 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
err.multipart_suggestion(msg, suggestions, applicability);
}
if let Some(ModuleOrUniformRoot::Module(module)) = module
&& let Some(module) = module.opt_def_id()
&& let Some(segment) = segment
{

if let Some(segment) = segment {
let module = match module {
Some(ModuleOrUniformRoot::Module(m)) if let Some(id) = m.opt_def_id() => id,
_ => CRATE_DEF_ID.to_def_id(),
};
self.find_cfg_stripped(&mut err, &segment, module);
}

Expand Down Expand Up @@ -2843,16 +2845,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
continue;
}

let note = errors::FoundItemConfigureOut { span: ident.span };
err.subdiagnostic(note);

if let CfgEntry::NameValue { value: Some((feature, _)), .. } = cfg.0 {
let note = errors::ItemWasBehindFeature { feature, span: cfg.1 };
err.subdiagnostic(note);
let item_was = if let CfgEntry::NameValue { value: Some((feature, _)), .. } = cfg.0 {
errors::ItemWas::BehindFeature { feature, span: cfg.1 }
} else {
let note = errors::ItemWasCfgOut { span: cfg.1 };
err.subdiagnostic(note);
}
errors::ItemWas::CfgOut { span: cfg.1 }
};
let note = errors::FoundItemConfigureOut { span: ident.span, item_was };
err.subdiagnostic(note);
}
}
}
Expand Down
52 changes: 32 additions & 20 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use rustc_errors::codes::*;
use rustc_errors::{Applicability, ElidedLifetimeInPathSubdiag, MultiSpan};
use rustc_errors::{
Applicability, Diag, ElidedLifetimeInPathSubdiag, EmissionGuarantee, IntoDiagArg, MultiSpan,
Subdiagnostic,
};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{Ident, Span, Symbol};

use crate::Res;
use crate::late::PatternSource;
use crate::{Res, fluent_generated as fluent};

#[derive(Diagnostic)]
#[diag(resolve_generic_params_from_outer_item, code = E0401)]
Expand Down Expand Up @@ -1201,26 +1204,35 @@ pub(crate) struct IdentInScopeButItIsDesc<'a> {
pub(crate) imported_ident_desc: &'a str,
}

#[derive(Subdiagnostic)]
#[note(resolve_found_an_item_configured_out)]
pub(crate) struct FoundItemConfigureOut {
#[primary_span]
pub(crate) span: Span,
}

#[derive(Subdiagnostic)]
#[note(resolve_item_was_behind_feature)]
pub(crate) struct ItemWasBehindFeature {
pub(crate) feature: Symbol,
#[primary_span]
pub(crate) span: Span,
}

#[derive(Subdiagnostic)]
#[note(resolve_item_was_cfg_out)]
pub(crate) struct ItemWasCfgOut {
#[primary_span]
pub(crate) span: Span,
pub(crate) item_was: ItemWas,
}

pub(crate) enum ItemWas {
BehindFeature { feature: Symbol, span: Span },
CfgOut { span: Span },
}

impl Subdiagnostic for FoundItemConfigureOut {
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
let mut multispan: MultiSpan = self.span.into();
match self.item_was {
ItemWas::BehindFeature { feature, span } => {
let key = "feature".into();
let value = feature.into_diag_arg(&mut None);
let msg = diag.dcx.eagerly_translate_to_string(
fluent::resolve_item_was_behind_feature,
[(&key, &value)].into_iter(),
);
multispan.push_span_label(span, msg);
}
ItemWas::CfgOut { span } => {
multispan.push_span_label(span, fluent::resolve_item_was_cfg_out);
}
}
diag.span_note(multispan, fluent::resolve_found_an_item_configured_out);
}
}

#[derive(Diagnostic)]
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4231,13 +4231,21 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
//
// And that's what happens below - we're just mixing both messages
// into a single one.
let failed_to_resolve = match parent_err.node {
ResolutionError::FailedToResolve { .. } => true,
_ => false,
};
let mut parent_err = this.r.into_struct_error(parent_err.span, parent_err.node);

// overwrite all properties with the parent's error message
err.messages = take(&mut parent_err.messages);
err.code = take(&mut parent_err.code);
swap(&mut err.span, &mut parent_err.span);
err.children = take(&mut parent_err.children);
if failed_to_resolve {
err.children = take(&mut parent_err.children);
} else {
err.children.append(&mut parent_err.children);
}
err.sort_span = parent_err.sort_span;
err.is_lint = parent_err.is_lint.clone();

Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}
self.err_code_special_cases(&mut err, source, path, span);

if let Some(module) = base_error.module {
self.r.find_cfg_stripped(&mut err, &path.last().unwrap().ident.name, module);
}
let module = base_error.module.unwrap_or_else(|| CRATE_DEF_ID.to_def_id());
self.r.find_cfg_stripped(&mut err, &path.last().unwrap().ident.name, module);

(err, candidates)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/cfg/both-true-false.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ error[E0425]: cannot find function `foo` in this scope
|
LL | foo();
| ^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/both-true-false.rs:6:4
|
LL | #[cfg(false)]
| ----- the item is gated here
LL | #[cfg(true)]
LL | fn foo() {}
| ^^^
note: found an item that was configured out
--> $DIR/both-true-false.rs:10:4
|
LL | #[cfg(false)]
| ----- the item is gated here
LL | fn foo() {}
| ^^^

error: aborting due to 1 previous error

Expand Down
90 changes: 90 additions & 0 deletions tests/ui/cfg/cfg-version/syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -128,60 +128,150 @@ error[E0425]: cannot find function `key_value_form` in this scope
|
LL | key_value_form();
| ^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:32:4
|
LL | #[cfg(version = "1.43")]
| ---------------- the item is gated behind the `1.43` feature
LL |
LL | fn key_value_form() {}
| ^^^^^^^^^^^^^^

error[E0425]: cannot find function `not_numbers_or_periods` in this scope
--> $DIR/syntax.rs:143:5
|
LL | not_numbers_or_periods();
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:53:4
|
LL | #[cfg(version("foo"))]
| ------- the item is gated here
LL |
LL | fn not_numbers_or_periods() {}
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `complex_semver_with_metadata` in this scope
--> $DIR/syntax.rs:144:5
|
LL | complex_semver_with_metadata();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:57:4
|
LL | #[cfg(version("1.20.0-stable"))]
| ----------------- the item is gated here
LL |
LL | fn complex_semver_with_metadata() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `invalid_major_only` in this scope
--> $DIR/syntax.rs:145:5
|
LL | invalid_major_only();
| ^^^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:80:4
|
LL | #[cfg(version("1"))]
| ----- the item is gated here
LL |
LL | fn invalid_major_only() {}
| ^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `invalid_major_only_zero` in this scope
--> $DIR/syntax.rs:146:5
|
LL | invalid_major_only_zero();
| ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:84:4
|
LL | #[cfg(version("0"))]
| ----- the item is gated here
LL |
LL | fn invalid_major_only_zero() {}
| ^^^^^^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `invalid_major_only_negative` in this scope
--> $DIR/syntax.rs:147:5
|
LL | invalid_major_only_negative();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:97:4
|
LL | #[cfg(version("-1"))]
| ------ the item is gated here
LL |
LL | fn invalid_major_only_negative() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `exceed_u16_major` in this scope
--> $DIR/syntax.rs:148:5
|
LL | exceed_u16_major();
| ^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:103:4
|
LL | #[cfg(version("65536"))]
| --------- the item is gated here
LL |
LL | fn exceed_u16_major() {}
| ^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `exceed_u16_minor` in this scope
--> $DIR/syntax.rs:149:5
|
LL | exceed_u16_minor();
| ^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:107:4
|
LL | #[cfg(version("1.65536.0"))]
| ------------- the item is gated here
LL |
LL | fn exceed_u16_minor() {}
| ^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `exceed_u16_patch` in this scope
--> $DIR/syntax.rs:150:5
|
LL | exceed_u16_patch();
| ^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:111:4
|
LL | #[cfg(version("1.0.65536"))]
| ------------- the item is gated here
LL |
LL | fn exceed_u16_patch() {}
| ^^^^^^^^^^^^^^^^

error[E0425]: cannot find function `exceed_u16_mixed` in this scope
--> $DIR/syntax.rs:151:5
|
LL | exceed_u16_mixed();
| ^^^^^^^^^^^^^^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/syntax.rs:115:4
|
LL | #[cfg(version("65536.0.65536"))]
| ----------------- the item is gated here
LL |
LL | fn exceed_u16_mixed() {}
| ^^^^^^^^^^^^^^^^

error: aborting due to 14 previous errors; 14 warnings emitted

Expand Down
8 changes: 8 additions & 0 deletions tests/ui/cfg/cmdline-false.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ error[E0425]: cannot find function `foo` in this scope
|
LL | foo();
| ^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/cmdline-false.rs:5:4
|
LL | #[cfg(false)]
| ----- the item is gated here
LL | fn foo() {}
| ^^^

error: aborting due to 1 previous error

Expand Down
4 changes: 0 additions & 4 deletions tests/ui/cfg/diagnostics-cross-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ fn main() {
cfged_out::inner::uwu(); //~ ERROR cannot find function
//~^ NOTE found an item that was configured out
//~| NOTE not found in `cfged_out::inner`
//~| NOTE the item is gated here

// The module isn't found - we would like to get a diagnostic, but currently don't due to
// the awkward way the resolver diagnostics are currently implemented.
cfged_out::inner::doesnt_exist::hello(); //~ ERROR failed to resolve
//~^ NOTE could not find `doesnt_exist` in `inner`
//~| NOTE found an item that was configured out
//~| NOTE the item is gated here

// It should find the one in the right module, not the wrong one.
cfged_out::inner::right::meow(); //~ ERROR cannot find function
//~^ NOTE found an item that was configured out
//~| NOTE not found in `cfged_out::inner::right
//~| NOTE the item is gated behind the `what-a-cool-feature` feature

// Exists in the crate root - diagnostic.
cfged_out::vanished(); //~ ERROR cannot find function
//~^ NOTE found an item that was configured out
//~| NOTE not found in `cfged_out`
//~| NOTE the item is gated here
}
Loading
Loading