Skip to content

Commit 67d5435

Browse files
Rollup merge of #85375 - SkiFire13:fix-85347, r=jackh726
Fix missing lifetimes diagnostics after #83759 In #83759 while rebasing I didn't realize there was a new function for suggesting to add lifetime arguments. It relied on some invariants, namely that if a generic type/trait has angle brackets then it must have some generic argument, which is now no longer true. This PR updates that function to handle the new invariants. This also adds a new regression test but I'm not sure if that's the correct place for it. Fixes #85347
2 parents ddc376c + 363eacd commit 67d5435

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+16-37
Original file line numberDiff line numberDiff line change
@@ -509,44 +509,23 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
509509
}
510510

511511
AngleBrackets::Available => {
512-
// angle brackets exist, so we insert missing arguments after the existing args
513-
514-
assert!(!self.gen_args.args.is_empty());
515-
516-
if self.num_provided_lifetime_args() > 0 {
517-
let last_lt_arg_span = self.gen_args.args
518-
[self.num_provided_lifetime_args() - 1]
519-
.span()
520-
.shrink_to_hi();
521-
let source_map = self.tcx.sess.source_map();
522-
523-
if let Ok(last_gen_arg) = source_map.span_to_snippet(last_lt_arg_span) {
524-
let sugg = format!("{}, {}", last_gen_arg, suggested_args);
525-
526-
err.span_suggestion_verbose(
527-
last_lt_arg_span,
528-
&msg,
529-
sugg,
530-
Applicability::HasPlaceholders,
531-
);
532-
}
512+
let (sugg_span, is_first) = if self.num_provided_lifetime_args() == 0 {
513+
(self.gen_args.span().unwrap().shrink_to_lo(), true)
533514
} else {
534-
// Non-lifetime arguments included in `gen_args` -> insert missing lifetimes before
535-
// existing arguments
536-
let first_arg_span = self.gen_args.args[0].span().shrink_to_lo();
537-
let source_map = self.tcx.sess.source_map();
538-
539-
if let Ok(first_gen_arg) = source_map.span_to_snippet(first_arg_span) {
540-
let sugg = format!("{}, {}", suggested_args, first_gen_arg);
541-
542-
err.span_suggestion_verbose(
543-
first_arg_span,
544-
&msg,
545-
sugg,
546-
Applicability::HasPlaceholders,
547-
);
548-
}
549-
}
515+
let last_lt = &self.gen_args.args[self.num_provided_lifetime_args() - 1];
516+
(last_lt.span().shrink_to_hi(), false)
517+
};
518+
let has_non_lt_args = self.num_provided_type_or_const_args() != 0;
519+
let has_bindings = !self.gen_args.bindings.is_empty();
520+
521+
let sugg_prefix = if is_first { "" } else { ", " };
522+
let sugg_suffix =
523+
if is_first && (has_non_lt_args || has_bindings) { ", " } else { "" };
524+
525+
let sugg = format!("{}{}{}", sugg_prefix, suggested_args, sugg_suffix);
526+
debug!("sugg: {:?}", sugg);
527+
528+
err.span_suggestion_verbose(sugg_span, &msg, sugg, Applicability::HasPlaceholders);
550529
}
551530
AngleBrackets::Implied => {
552531
// We never encounter missing lifetimes in situations in which lifetimes are elided
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![allow(incomplete_features)]
2+
#![feature(generic_associated_types)]
3+
use std::ops::Deref;
4+
trait Foo {
5+
type Bar<'a>: Deref<Target = <Self>::Bar<Target = Self>>;
6+
//~^ ERROR this associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
7+
//~| HELP add missing
8+
}
9+
10+
fn main() {}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0107]: this associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
2+
--> $DIR/issue-85347.rs:5:42
3+
|
4+
LL | type Bar<'a>: Deref<Target = <Self>::Bar<Target = Self>>;
5+
| ^^^ expected 1 lifetime argument
6+
|
7+
note: associated type defined here, with 1 lifetime parameter: `'a`
8+
--> $DIR/issue-85347.rs:5:10
9+
|
10+
LL | type Bar<'a>: Deref<Target = <Self>::Bar<Target = Self>>;
11+
| ^^^ --
12+
help: add missing lifetime argument
13+
|
14+
LL | type Bar<'a>: Deref<Target = <Self>::Bar<'a, Target = Self>>;
15+
| ^^^
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)