Skip to content
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

Don't allow consts with unconstrained lifetimes in their types #114713

Closed
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
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/impl_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
&mut input_parameters,
);

// Disallow unconstrained lifetimes, but only if they appear in assoc types.
// Disallow unconstrained lifetimes, but only if they appear in assoc types or consts.
let lifetimes_in_associated_types: FxHashSet<_> = tcx
.associated_item_def_ids(impl_def_id)
.iter()
.flat_map(|def_id| {
let item = tcx.associated_item(def_id);
match item.kind {
ty::AssocKind::Type => {
ty::AssocKind::Type | ty::AssocKind::Const => {
if item.defaultness(tcx).has_value() {
cgp::parameters_for(&tcx.type_of(def_id).instantiate_identity(), true)
} else {
vec![]
}
}
ty::AssocKind::Fn | ty::AssocKind::Const => vec![],
ty::AssocKind::Fn => vec![],
}
})
.collect();
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/consts/assoc-const-unconstrained-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Foo;

impl<'a> Foo {
const CONST: &'a str = "";
//~^ ERROR the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
}

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/consts/assoc-const-unconstrained-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> $DIR/assoc-const-unconstrained-lifetime.rs:3:6
|
LL | impl<'a> Foo {
| ^^ unconstrained lifetime parameter

error: aborting due to previous error

For more information about this error, try `rustc --explain E0207`.
2 changes: 1 addition & 1 deletion tests/ui/nll/trait-associated-constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct FailStruct { }

impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
const AC: Option<&'c str> = None;
//~^ ERROR: const not compatible with trait
//~^ ERROR: the lifetime parameter `'c` is not constrained by the impl trait, self type, or predicates
}

struct OKStruct2 { }
Expand Down
19 changes: 3 additions & 16 deletions tests/ui/nll/trait-associated-constant.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
error[E0308]: const not compatible with trait
--> $DIR/trait-associated-constant.rs:21:5
|
LL | const AC: Option<&'c str> = None;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected enum `Option<&'b str>`
found enum `Option<&'c str>`
note: the lifetime `'c` as defined here...
error[E0207]: the lifetime parameter `'c` is not constrained by the impl trait, self type, or predicates
--> $DIR/trait-associated-constant.rs:20:18
|
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
| ^^
note: ...does not necessarily outlive the lifetime `'b` as defined here
--> $DIR/trait-associated-constant.rs:20:14
|
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
| ^^
| ^^ unconstrained lifetime parameter

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
For more information about this error, try `rustc --explain E0207`.
Loading