Skip to content

Commit 92dbfcc

Browse files
authored
Rollup merge of rust-lang#135000 - compiler-errors:opaque-captures-dupe, r=lqd
Fix ICE when opaque captures a duplicated/invalid lifetime See description on test. Fixes rust-lang#132766 Fixes rust-lang#133693 Fixes rust-lang#134780
2 parents fac31a1 + d3c6067 commit 92dbfcc

File tree

8 files changed

+53
-27
lines changed

8 files changed

+53
-27
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1845,11 +1845,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18451845
GenericParamKind::Lifetime => {
18461846
// AST resolution emitted an error on those parameters, so we lower them using
18471847
// `ParamName::Error`.
1848+
let ident = self.lower_ident(param.ident);
18481849
let param_name =
18491850
if let Some(LifetimeRes::Error) = self.resolver.get_lifetime_res(param.id) {
1850-
ParamName::Error
1851+
ParamName::Error(ident)
18511852
} else {
1852-
let ident = self.lower_ident(param.ident);
18531853
ParamName::Plain(ident)
18541854
};
18551855
let kind =

Diff for: compiler/rustc_hir/src/hir.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ pub enum ParamName {
5252
/// Some user-given name like `T` or `'x`.
5353
Plain(Ident),
5454

55+
/// Indicates an illegal name was given and an error has been
56+
/// reported (so we should squelch other derived errors).
57+
///
58+
/// Occurs when, e.g., `'_` is used in the wrong place, or a
59+
/// lifetime name is duplicated.
60+
Error(Ident),
61+
5562
/// Synthetic name generated when user elided a lifetime in an impl header.
5663
///
5764
/// E.g., the lifetimes in cases like these:
@@ -67,18 +74,13 @@ pub enum ParamName {
6774
/// where `'f` is something like `Fresh(0)`. The indices are
6875
/// unique per impl, but not necessarily continuous.
6976
Fresh,
70-
71-
/// Indicates an illegal name was given and an error has been
72-
/// reported (so we should squelch other derived errors). Occurs
73-
/// when, e.g., `'_` is used in the wrong place.
74-
Error,
7577
}
7678

7779
impl ParamName {
7880
pub fn ident(&self) -> Ident {
7981
match *self {
80-
ParamName::Plain(ident) => ident,
81-
ParamName::Fresh | ParamName::Error => Ident::with_dummy_span(kw::UnderscoreLifetime),
82+
ParamName::Plain(ident) | ParamName::Error(ident) => ident,
83+
ParamName::Fresh => Ident::with_dummy_span(kw::UnderscoreLifetime),
8284
}
8385
}
8486
}

Diff for: compiler/rustc_hir/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,8 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
928928
) -> V::Result {
929929
try_visit!(visitor.visit_id(param.hir_id));
930930
match param.name {
931-
ParamName::Plain(ident) => try_visit!(visitor.visit_ident(ident)),
932-
ParamName::Error | ParamName::Fresh => {}
931+
ParamName::Plain(ident) | ParamName::Error(ident) => try_visit!(visitor.visit_ident(ident)),
932+
ParamName::Fresh => {}
933933
}
934934
match param.kind {
935935
GenericParamKind::Lifetime { .. } => {}

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,10 @@ fn check_variances_for_type_defn<'tcx>(
20072007
}
20082008

20092009
match hir_param.name {
2010-
hir::ParamName::Error => {}
2010+
hir::ParamName::Error(_) => {
2011+
// Don't report a bivariance error for a lifetime that isn't
2012+
// even valid to name.
2013+
}
20112014
_ => {
20122015
let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
20132016
report_bivariance(tcx, hir_param, has_explicit_bounds, item);

Diff for: tests/crashes/132766.rs

-9
This file was deleted.

Diff for: tests/ui/impl-trait/captured-invalid-lifetime.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This uses edition 2024 for new lifetime capture rules.
2+
//@ edition: 2024
3+
4+
// The problem here is that the presence of the opaque which captures all lifetimes in scope
5+
// means that the duplicated `'a` (which I'll call the dupe) is considered to be *early-bound*
6+
// since it shows up in the output but not the inputs. This is paired with the fact that we
7+
// were previously setting the name of the dupe to `'_` in the generic param definition, which
8+
// means that the identity args for the function were `['a#0, '_#1]` even though the lifetime
9+
// for the dupe should've been `'a#1`. This difference in symbol meant that NLL couldn't
10+
// actually match the lifetime against the identity lifetimes, leading to an ICE.
11+
12+
struct Foo<'a>(&'a ());
13+
14+
impl<'a> Foo<'a> {
15+
fn pass<'a>() -> impl Sized {}
16+
//~^ ERROR lifetime name `'a` shadows a lifetime name that is already in scope
17+
}
18+
19+
fn main() {}

Diff for: tests/ui/impl-trait/captured-invalid-lifetime.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
2+
--> $DIR/captured-invalid-lifetime.rs:15:13
3+
|
4+
LL | impl<'a> Foo<'a> {
5+
| -- first declared here
6+
LL | fn pass<'a>() -> impl Sized {}
7+
| ^^ lifetime `'a` already in scope
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0496`.

Diff for: tests/ui/stats/input-stats.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ hir-stats Variant 144 ( 1.6%) 2 72
146146
hir-stats GenericBound 256 ( 2.9%) 4 64
147147
hir-stats - Trait 256 ( 2.9%) 4
148148
hir-stats Block 288 ( 3.2%) 6 48
149-
hir-stats GenericParam 360 ( 4.0%) 5 72
150149
hir-stats Pat 360 ( 4.0%) 5 72
151150
hir-stats - Struct 72 ( 0.8%) 1
152151
hir-stats - Wild 72 ( 0.8%) 1
153152
hir-stats - Binding 216 ( 2.4%) 3
154-
hir-stats Generics 560 ( 6.3%) 10 56
155-
hir-stats Ty 720 ( 8.1%) 15 48
153+
hir-stats GenericParam 400 ( 4.5%) 5 80
154+
hir-stats Generics 560 ( 6.2%) 10 56
155+
hir-stats Ty 720 ( 8.0%) 15 48
156156
hir-stats - Ptr 48 ( 0.5%) 1
157157
hir-stats - Ref 48 ( 0.5%) 1
158158
hir-stats - Path 624 ( 7.0%) 13
@@ -171,8 +171,8 @@ hir-stats - Impl 88 ( 1.0%) 1
171171
hir-stats - Trait 88 ( 1.0%) 1
172172
hir-stats - Fn 176 ( 2.0%) 2
173173
hir-stats - Use 352 ( 3.9%) 4
174-
hir-stats Path 1_240 (13.9%) 31 40
175-
hir-stats PathSegment 1_920 (21.5%) 40 48
174+
hir-stats Path 1_240 (13.8%) 31 40
175+
hir-stats PathSegment 1_920 (21.4%) 40 48
176176
hir-stats ----------------------------------------------------------------
177-
hir-stats Total 8_936 180
177+
hir-stats Total 8_976 180
178178
hir-stats

0 commit comments

Comments
 (0)