Skip to content

Commit 3f7b1a5

Browse files
committed
Clean up some things in the name resolver
* Get rid of a typo in a function name * Rename `currently_processing_generics`: The old name confused me at first since I assumed it referred to generic *parameters* when it was in fact referring to generic *arguments*. Generics are typically short for generic params. * Get rid of a few unwraps by properly leveraging slice patterns
1 parent cb4d9a1 commit 3f7b1a5

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

compiler/rustc_resolve/src/late.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ struct DiagnosticMetadata<'ast> {
594594
/// The current trait (used to suggest).
595595
current_item: Option<&'ast Item>,
596596

597-
/// When processing generics and encountering a type not found, suggest introducing a type
598-
/// param.
599-
currently_processing_generics: bool,
597+
/// When processing generic arguments and encountering an unresolved ident not found,
598+
/// suggest introducing a type or const param depending on the context.
599+
currently_processing_generic_args: bool,
600600

601601
/// The current enclosing (non-closure) function (used for better errors).
602602
current_function: Option<(FnKind<'ast>, Span)>,
@@ -1069,7 +1069,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
10691069

10701070
fn visit_generic_arg(&mut self, arg: &'ast GenericArg) {
10711071
debug!("visit_generic_arg({:?})", arg);
1072-
let prev = replace(&mut self.diagnostic_metadata.currently_processing_generics, true);
1072+
let prev = replace(&mut self.diagnostic_metadata.currently_processing_generic_args, true);
10731073
match arg {
10741074
GenericArg::Type(ref ty) => {
10751075
// We parse const arguments as path types as we cannot distinguish them during
@@ -1100,7 +1100,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11001100
},
11011101
);
11021102

1103-
self.diagnostic_metadata.currently_processing_generics = prev;
1103+
self.diagnostic_metadata.currently_processing_generic_args = prev;
11041104
return;
11051105
}
11061106
}
@@ -1113,7 +1113,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11131113
self.resolve_anon_const(ct, AnonConstKind::ConstArg(IsRepeatExpr::No))
11141114
}
11151115
}
1116-
self.diagnostic_metadata.currently_processing_generics = prev;
1116+
self.diagnostic_metadata.currently_processing_generic_args = prev;
11171117
}
11181118

11191119
fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {

compiler/rustc_resolve/src/late/diagnostics.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
452452
}
453453

454454
self.suggest_self_or_self_ref(&mut err, path, span);
455-
self.detect_assoct_type_constraint_meant_as_path(&mut err, &base_error);
455+
self.detect_assoc_type_constraint_meant_as_path(&mut err, &base_error);
456456
if self.suggest_self_ty(&mut err, source, path, span)
457457
|| self.suggest_self_value(&mut err, source, path, span)
458458
{
@@ -491,7 +491,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
491491
(err, candidates)
492492
}
493493

494-
fn detect_assoct_type_constraint_meant_as_path(
494+
fn detect_assoc_type_constraint_meant_as_path(
495495
&self,
496496
err: &mut Diagnostic,
497497
base_error: &BaseError,
@@ -799,17 +799,19 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
799799
false,
800800
) = (source, res, is_macro)
801801
{
802-
if let Some(bounds @ [_, .., _]) = self.diagnostic_metadata.current_trait_object {
802+
if let Some(bounds @ [first_bound, .., last_bound]) =
803+
self.diagnostic_metadata.current_trait_object
804+
{
803805
fallback = true;
804806
let spans: Vec<Span> = bounds
805807
.iter()
806808
.map(|bound| bound.span())
807809
.filter(|&sp| sp != base_error.span)
808810
.collect();
809811

810-
let start_span = bounds[0].span();
812+
let start_span = first_bound.span();
811813
// `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
812-
let end_span = bounds.last().unwrap().span();
814+
let end_span = last_bound.span();
813815
// `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
814816
let last_bound_span = spans.last().cloned().unwrap();
815817
let mut multi_span: MultiSpan = spans.clone().into();
@@ -2419,10 +2421,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
24192421
let mut iter = ident.chars().map(|c| c.is_uppercase());
24202422
let single_uppercase_char =
24212423
matches!(iter.next(), Some(true)) && matches!(iter.next(), None);
2422-
if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
2424+
if !self.diagnostic_metadata.currently_processing_generic_args && !single_uppercase_char {
24232425
return None;
24242426
}
2425-
match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generics) {
2427+
match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generic_args) {
24262428
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => {
24272429
// Ignore `fn main()` as we don't want to suggest `fn main<T>()`
24282430
}

0 commit comments

Comments
 (0)