Skip to content

Commit 4d1c273

Browse files
authored
Rollup merge of #99249 - cjgillot:no-reparse-fn, r=fee1-dead
Do not re-parse function signatures to suggest generics This PR uses the existing resolution rib infrastructure to channel the correct span information to suggest generic parameters. This allows to avoid re-parsing a function's source code. Drive-by cleanup: this removes useless `FnItemRibKind` from late resolution ribs. All the use cases are already covered by `ItemRibKind` and `AssocItemRibKind` which have more precise semantics.
2 parents 1cff564 + dff4280 commit 4d1c273

18 files changed

+134
-218
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl<'a> Resolver<'a> {
511511
err.span_label(span, "use of generic parameter from outer function");
512512

513513
let sm = self.session.source_map();
514-
match outer_res {
514+
let def_id = match outer_res {
515515
Res::SelfTy { trait_: maybe_trait_defid, alias_to: maybe_impl_defid } => {
516516
if let Some(impl_span) =
517517
maybe_impl_defid.and_then(|(def_id, _)| self.opt_span(def_id))
@@ -536,40 +536,37 @@ impl<'a> Resolver<'a> {
536536
if let Some(span) = self.opt_span(def_id) {
537537
err.span_label(span, "type parameter from outer function");
538538
}
539+
def_id
539540
}
540541
Res::Def(DefKind::ConstParam, def_id) => {
541542
if let Some(span) = self.opt_span(def_id) {
542543
err.span_label(span, "const parameter from outer function");
543544
}
545+
def_id
544546
}
545547
_ => {
546548
bug!(
547549
"GenericParamsFromOuterFunction should only be used with Res::SelfTy, \
548550
DefKind::TyParam or DefKind::ConstParam"
549551
);
550552
}
551-
}
553+
};
552554

553-
if has_generic_params == HasGenericParams::Yes {
555+
if let HasGenericParams::Yes(span) = has_generic_params {
554556
// Try to retrieve the span of the function signature and generate a new
555557
// message with a local type or const parameter.
556558
let sugg_msg = "try using a local generic parameter instead";
557-
if let Some((sugg_span, snippet)) = sm.generate_local_type_param_snippet(span) {
558-
// Suggest the modification to the user
559-
err.span_suggestion(
560-
sugg_span,
561-
sugg_msg,
562-
snippet,
563-
Applicability::MachineApplicable,
564-
);
565-
} else if let Some(sp) = sm.generate_fn_name_span(span) {
566-
err.span_label(
567-
sp,
568-
"try adding a local generic parameter in this method instead",
569-
);
559+
let name = self.opt_name(def_id).unwrap_or(sym::T);
560+
let (span, snippet) = if span.is_empty() {
561+
let snippet = format!("<{}>", name);
562+
(span, snippet)
570563
} else {
571-
err.help("try using a local generic parameter instead");
572-
}
564+
let span = sm.span_through_char(span, '<').shrink_to_hi();
565+
let snippet = format!("{}, ", name);
566+
(span, snippet)
567+
};
568+
// Suggest the modification to the user
569+
err.span_suggestion(span, sugg_msg, snippet, Applicability::MaybeIncorrect);
573570
}
574571

575572
err

compiler/rustc_resolve/src/ident.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use rustc_span::{Span, DUMMY_SP};
1313

1414
use std::ptr;
1515

16-
use crate::late::{ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind};
16+
use crate::late::{
17+
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
18+
};
1719
use crate::macros::{sub_namespace_match, MacroRulesScope};
1820
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
1921
use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
@@ -1103,7 +1105,7 @@ impl<'a> Resolver<'a> {
11031105
| ForwardGenericParamBanRibKind => {
11041106
// Nothing to do. Continue.
11051107
}
1106-
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
1108+
ItemRibKind(_) | AssocItemRibKind => {
11071109
// This was an attempt to access an upvar inside a
11081110
// named function item. This is not allowed, so we
11091111
// report an error.
@@ -1168,10 +1170,10 @@ impl<'a> Resolver<'a> {
11681170
let has_generic_params: HasGenericParams = match rib.kind {
11691171
NormalRibKind
11701172
| ClosureOrAsyncRibKind
1171-
| AssocItemRibKind
11721173
| ModuleRibKind(..)
11731174
| MacroDefinition(..)
11741175
| InlineAsmSymRibKind
1176+
| AssocItemRibKind
11751177
| ForwardGenericParamBanRibKind => {
11761178
// Nothing to do. Continue.
11771179
continue;
@@ -1180,7 +1182,9 @@ impl<'a> Resolver<'a> {
11801182
ConstantItemRibKind(trivial, _) => {
11811183
let features = self.session.features_untracked();
11821184
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1183-
if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1185+
if !(trivial == ConstantHasGenerics::Yes
1186+
|| features.generic_const_exprs)
1187+
{
11841188
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
11851189
// we can't easily tell if it's generic at this stage, so we instead remember
11861190
// this and then enforce the self type to be concrete later on.
@@ -1207,7 +1211,6 @@ impl<'a> Resolver<'a> {
12071211

12081212
// This was an attempt to use a type parameter outside its scope.
12091213
ItemRibKind(has_generic_params) => has_generic_params,
1210-
FnItemRibKind => HasGenericParams::Yes,
12111214
ConstParamTyRibKind => {
12121215
if let Some(span) = finalize {
12131216
self.report_error(
@@ -1232,28 +1235,22 @@ impl<'a> Resolver<'a> {
12321235
}
12331236
}
12341237
Res::Def(DefKind::ConstParam, _) => {
1235-
let mut ribs = ribs.iter().peekable();
1236-
if let Some(Rib { kind: FnItemRibKind, .. }) = ribs.peek() {
1237-
// When declaring const parameters inside function signatures, the first rib
1238-
// is always a `FnItemRibKind`. In this case, we can skip it, to avoid it
1239-
// (spuriously) conflicting with the const param.
1240-
ribs.next();
1241-
}
1242-
12431238
for rib in ribs {
12441239
let has_generic_params = match rib.kind {
12451240
NormalRibKind
12461241
| ClosureOrAsyncRibKind
1247-
| AssocItemRibKind
12481242
| ModuleRibKind(..)
12491243
| MacroDefinition(..)
12501244
| InlineAsmSymRibKind
1245+
| AssocItemRibKind
12511246
| ForwardGenericParamBanRibKind => continue,
12521247

12531248
ConstantItemRibKind(trivial, _) => {
12541249
let features = self.session.features_untracked();
12551250
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1256-
if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1251+
if !(trivial == ConstantHasGenerics::Yes
1252+
|| features.generic_const_exprs)
1253+
{
12571254
if let Some(span) = finalize {
12581255
self.report_error(
12591256
span,
@@ -1272,7 +1269,6 @@ impl<'a> Resolver<'a> {
12721269
}
12731270

12741271
ItemRibKind(has_generic_params) => has_generic_params,
1275-
FnItemRibKind => HasGenericParams::Yes,
12761272
ConstParamTyRibKind => {
12771273
if let Some(span) = finalize {
12781274
self.report_error(

0 commit comments

Comments
 (0)