Skip to content

Commit 3940146

Browse files
committed
Auto merge of #60892 - davidtwco:issue-60622, r=oli-obk
Checking generic args after late bound region err. Fixes #60622. This PR fixes an ICE that occurs when a late bound region error is emitted and that resulted in the rest of the generic arguments of a function not being checked. For example, you could specify a generic type parameter `T` in a function call `foo<'_, T>()` to a function that doesn't have a generic type parameter. Since an error wasn't emitted from the function, compilation continued to parts of typeck that didn't expect a generic type argument in a call for a function that didn't have any generic type arguments.
2 parents 68fd80f + bff8a86 commit 3940146

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/librustc_typeck/astconv.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
290290
}
291291

292292
// Prohibit explicit lifetime arguments if late-bound lifetime parameters are present.
293+
let mut reported_late_bound_region_err = None;
293294
if !infer_lifetimes {
294295
if let Some(span_late) = def.has_late_bound_regions {
295296
let msg = "cannot specify lifetime arguments explicitly \
@@ -301,13 +302,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
301302
let mut err = tcx.sess.struct_span_err(span, msg);
302303
err.span_note(span_late, note);
303304
err.emit();
304-
return (true, None);
305+
reported_late_bound_region_err = Some(true);
305306
} else {
306307
let mut multispan = MultiSpan::from_span(span);
307308
multispan.push_span_label(span_late, note.to_string());
308309
tcx.lint_hir(lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS,
309310
args.args[0].id(), multispan, msg);
310-
return (false, None);
311+
reported_late_bound_region_err = Some(false);
311312
}
312313
}
313314
}
@@ -325,7 +326,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
325326
// For kinds without defaults (i.e., lifetimes), `required == permitted`.
326327
// For other kinds (i.e., types), `permitted` may be greater than `required`.
327328
if required <= provided && provided <= permitted {
328-
return (false, None);
329+
return (reported_late_bound_region_err.unwrap_or(false), None);
329330
}
330331

331332
// Unfortunately lifetime and type parameter mismatches are typically styled
@@ -380,7 +381,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
380381
potential_assoc_types)
381382
};
382383

383-
if !infer_lifetimes || arg_counts.lifetimes > param_counts.lifetimes {
384+
if reported_late_bound_region_err.is_none()
385+
&& (!infer_lifetimes || arg_counts.lifetimes > param_counts.lifetimes) {
384386
check_kind_count(
385387
"lifetime",
386388
param_counts.lifetimes,
@@ -410,7 +412,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
410412
arg_counts.lifetimes,
411413
)
412414
} else {
413-
(false, None)
415+
(reported_late_bound_region_err.unwrap_or(false), None)
414416
}
415417
}
416418

src/test/ui/issue-60622.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ignore-tidy-linelength
2+
3+
#![deny(warnings)]
4+
5+
struct Borked {}
6+
7+
impl Borked {
8+
fn a(&self) {}
9+
}
10+
11+
fn run_wild<T>(b: &Borked) {
12+
b.a::<'_, T>();
13+
//~^ ERROR cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
14+
//~^^ ERROR wrong number of type arguments: expected 0, found 1
15+
//~^^^ WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
16+
}
17+
18+
fn main() {}

src/test/ui/issue-60622.stderr

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
2+
--> $DIR/issue-60622.rs:12:11
3+
|
4+
LL | fn a(&self) {}
5+
| - the late bound lifetime parameter is introduced here
6+
...
7+
LL | b.a::<'_, T>();
8+
| ^^
9+
|
10+
note: lint level defined here
11+
--> $DIR/issue-60622.rs:3:9
12+
|
13+
LL | #![deny(warnings)]
14+
| ^^^^^^^^
15+
= note: #[deny(late_bound_lifetime_arguments)] implied by #[deny(warnings)]
16+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
17+
= note: for more information, see issue #42868 <https://github.com/rust-lang/rust/issues/42868>
18+
19+
error[E0107]: wrong number of type arguments: expected 0, found 1
20+
--> $DIR/issue-60622.rs:12:15
21+
|
22+
LL | b.a::<'_, T>();
23+
| ^ unexpected type argument
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)