Skip to content

Fix ICE when opaque captures a duplicated/invalid lifetime #135000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1845,11 +1845,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericParamKind::Lifetime => {
// AST resolution emitted an error on those parameters, so we lower them using
// `ParamName::Error`.
let ident = self.lower_ident(param.ident);
let param_name =
if let Some(LifetimeRes::Error) = self.resolver.get_lifetime_res(param.id) {
ParamName::Error
ParamName::Error(ident)
} else {
let ident = self.lower_ident(param.ident);
ParamName::Plain(ident)
};
let kind =
Expand Down
16 changes: 9 additions & 7 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ pub enum ParamName {
/// Some user-given name like `T` or `'x`.
Plain(Ident),

/// Indicates an illegal name was given and an error has been
/// reported (so we should squelch other derived errors).
///
/// Occurs when, e.g., `'_` is used in the wrong place, or a
/// lifetime name is duplicated.
Error(Ident),

/// Synthetic name generated when user elided a lifetime in an impl header.
///
/// E.g., the lifetimes in cases like these:
Expand All @@ -67,18 +74,13 @@ pub enum ParamName {
/// where `'f` is something like `Fresh(0)`. The indices are
/// unique per impl, but not necessarily continuous.
Fresh,

/// Indicates an illegal name was given and an error has been
/// reported (so we should squelch other derived errors). Occurs
/// when, e.g., `'_` is used in the wrong place.
Error,
}

impl ParamName {
pub fn ident(&self) -> Ident {
match *self {
ParamName::Plain(ident) => ident,
ParamName::Fresh | ParamName::Error => Ident::with_dummy_span(kw::UnderscoreLifetime),
ParamName::Plain(ident) | ParamName::Error(ident) => ident,
ParamName::Fresh => Ident::with_dummy_span(kw::UnderscoreLifetime),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
) -> V::Result {
try_visit!(visitor.visit_id(param.hir_id));
match param.name {
ParamName::Plain(ident) => try_visit!(visitor.visit_ident(ident)),
ParamName::Error | ParamName::Fresh => {}
ParamName::Plain(ident) | ParamName::Error(ident) => try_visit!(visitor.visit_ident(ident)),
ParamName::Fresh => {}
}
match param.kind {
GenericParamKind::Lifetime { .. } => {}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,10 @@ fn check_variances_for_type_defn<'tcx>(
}

match hir_param.name {
hir::ParamName::Error => {}
hir::ParamName::Error(_) => {
// Don't report a bivariance error for a lifetime that isn't
// even valid to name.
}
_ => {
let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
report_bivariance(tcx, hir_param, has_explicit_bounds, item);
Expand Down
9 changes: 0 additions & 9 deletions tests/crashes/132766.rs

This file was deleted.

19 changes: 19 additions & 0 deletions tests/ui/impl-trait/captured-invalid-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This uses edition 2024 for new lifetime capture rules.
//@ edition: 2024

// The problem here is that the presence of the opaque which captures all lifetimes in scope
// means that the duplicated `'a` (which I'll call the dupe) is considered to be *early-bound*
// since it shows up in the output but not the inputs. This is paired with the fact that we
// were previously setting the name of the dupe to `'_` in the generic param definition, which
// means that the identity args for the function were `['a#0, '_#1]` even though the lifetime
// for the dupe should've been `'a#1`. This difference in symbol meant that NLL couldn't
// actually match the lifetime against the identity lifetimes, leading to an ICE.

struct Foo<'a>(&'a ());

impl<'a> Foo<'a> {
fn pass<'a>() -> impl Sized {}
//~^ ERROR lifetime name `'a` shadows a lifetime name that is already in scope
}

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/impl-trait/captured-invalid-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
--> $DIR/captured-invalid-lifetime.rs:15:13
|
LL | impl<'a> Foo<'a> {
| -- first declared here
LL | fn pass<'a>() -> impl Sized {}
| ^^ lifetime `'a` already in scope

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0496`.
12 changes: 6 additions & 6 deletions tests/ui/stats/input-stats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ hir-stats Variant 144 ( 1.6%) 2 72
hir-stats GenericBound 256 ( 2.9%) 4 64
hir-stats - Trait 256 ( 2.9%) 4
hir-stats Block 288 ( 3.2%) 6 48
hir-stats GenericParam 360 ( 4.0%) 5 72
hir-stats Pat 360 ( 4.0%) 5 72
hir-stats - Struct 72 ( 0.8%) 1
hir-stats - Wild 72 ( 0.8%) 1
hir-stats - Binding 216 ( 2.4%) 3
hir-stats Generics 560 ( 6.3%) 10 56
hir-stats Ty 720 ( 8.1%) 15 48
hir-stats GenericParam 400 ( 4.5%) 5 80
hir-stats Generics 560 ( 6.2%) 10 56
hir-stats Ty 720 ( 8.0%) 15 48
hir-stats - Ptr 48 ( 0.5%) 1
hir-stats - Ref 48 ( 0.5%) 1
hir-stats - Path 624 ( 7.0%) 13
Expand All @@ -171,8 +171,8 @@ hir-stats - Impl 88 ( 1.0%) 1
hir-stats - Trait 88 ( 1.0%) 1
hir-stats - Fn 176 ( 2.0%) 2
hir-stats - Use 352 ( 3.9%) 4
hir-stats Path 1_240 (13.9%) 31 40
hir-stats PathSegment 1_920 (21.5%) 40 48
hir-stats Path 1_240 (13.8%) 31 40
hir-stats PathSegment 1_920 (21.4%) 40 48
hir-stats ----------------------------------------------------------------
hir-stats Total 8_936 180
hir-stats Total 8_976 180
hir-stats
Loading