Skip to content
Draft
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
12 changes: 12 additions & 0 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5607,6 +5607,18 @@ impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, 'ast, '_, '_> {
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime { .. }))
.count();
self.r.item_generics_num_lifetimes.insert(def_id, count);
let type_or_const_count = generics
.params
.iter()
.filter(|param| {
matches!(
param.kind,
ast::GenericParamKind::Type { .. }
| ast::GenericParamKind::Const { .. }
)
})
.count();
self.r.item_generics_num_type_or_const_params.insert(def_id, type_or_const_count);
}

ItemKind::ForeignMod(ForeignMod { items, .. }) => {
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,17 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
let mut fallback = false;
let typo_sugg = typo_sugg
.to_opt_suggestion()
.filter(|sugg| !suggested_candidates.contains(sugg.candidate.as_str()));
.filter(|sugg| !suggested_candidates.contains(sugg.candidate.as_str()))
// this filters out suggestions that require parameters when no parameter is present
.filter(|sugg| {
if !path.last().is_some_and(|s| s.has_generic_args) {
return true;
}
let Some(def_id) = sugg.res.opt_def_id() else {
return true;
};
self.r.item_generics_num_type_or_const_params(def_id) > 0
});
if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
fallback = true;
match self.diag_metadata.current_let_binding {
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,8 @@ pub struct Resolver<'ra, 'tcx> {

/// Amount of lifetime parameters for each item in the crate.
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize> = default::fx_hash_map(),
/// Amount of type or const parameters for each item in the crate.
item_generics_num_type_or_const_params: FxHashMap<LocalDefId, usize> = default::fx_hash_map(),
/// Generic args to suggest for required params (e.g. `<'_>`, `<_, _>`), if any.
item_required_generic_args_suggestions: FxHashMap<LocalDefId, String> = default::fx_hash_map(),
delegation_fn_sigs: LocalDefIdMap<DelegationFnSig> = Default::default(),
Expand Down Expand Up @@ -1709,6 +1711,17 @@ impl<'tcx> Resolver<'_, 'tcx> {
self.tcx.generics_of(def_id).own_counts().lifetimes
}
}
// Get count of expected parameters
fn item_generics_num_type_or_const_params(&self, def_id: DefId) -> usize {
if let Some(def_id) = def_id.as_local() {
self.item_generics_num_type_or_const_params.get(&def_id).copied().unwrap_or(0)
} else {
let generics = self.tcx.generics_of(def_id);
let counts = generics.own_counts();
// saturating_sub may not be strictly necessary, better safe than sorry
counts.types.saturating_sub(generics.has_own_self() as usize) + counts.consts
}
}

fn item_required_generic_args_suggestion(&self, def_id: DefId) -> String {
if let Some(def_id) = def_id.as_local() {
Expand Down
11 changes: 1 addition & 10 deletions tests/ui/const-generics/mgca/unused_speculative_def_id.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ error[E0425]: cannot find value `ERR` in this scope
--> $DIR/unused_speculative_def_id.rs:20:16
|
LL | field: A<{ ERR::<const { 1 }> }>,
| ^^^
|
--> $SRC_DIR/core/src/result.rs:LL:COL
|
= note: similarly named tuple variant `Err` defined here
help: a tuple variant with a similar name exists
|
LL - field: A<{ ERR::<const { 1 }> }>,
LL + field: A<{ Err::<const { 1 }> }>,
|
| ^^^ not found in this scope

error: aborting due to 1 previous error

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/imports/generic-param-filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn test_cloned(_x: cloned<(), ()>){}
//~^ ERROR: cannot find type `cloned` in this scope

fn main(){}
9 changes: 9 additions & 0 deletions tests/ui/imports/generic-param-filter.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0425]: cannot find type `cloned` in this scope
--> $DIR/generic-param-filter.rs:1:20
|
LL | fn test_cloned(_x: cloned<(), ()>){}
| ^^^^^^ not found in this scope

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.
Loading