Skip to content

Commit e179d5d

Browse files
Don't allow consts with unconstrained lifetimes in their types
1 parent faee636 commit e179d5d

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,21 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
9494
&mut input_parameters,
9595
);
9696

97-
// Disallow unconstrained lifetimes, but only if they appear in assoc types.
97+
// Disallow unconstrained lifetimes, but only if they appear in assoc types or consts.
9898
let lifetimes_in_associated_types: FxHashSet<_> = tcx
9999
.associated_item_def_ids(impl_def_id)
100100
.iter()
101101
.flat_map(|def_id| {
102102
let item = tcx.associated_item(def_id);
103103
match item.kind {
104-
ty::AssocKind::Type => {
104+
ty::AssocKind::Type | ty::AssocKind::Const => {
105105
if item.defaultness(tcx).has_value() {
106106
cgp::parameters_for(&tcx.type_of(def_id).instantiate_identity(), true)
107107
} else {
108108
vec![]
109109
}
110110
}
111-
ty::AssocKind::Fn | ty::AssocKind::Const => vec![],
111+
ty::AssocKind::Fn => vec![],
112112
}
113113
})
114114
.collect();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Foo;
2+
3+
impl<'a> Foo {
4+
const CONST: &'a str = "";
5+
//~^ ERROR the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/assoc-const-unconstrained-lifetime.rs:3:6
3+
|
4+
LL | impl<'a> Foo {
5+
| ^^ unconstrained lifetime parameter
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0207`.

tests/ui/nll/trait-associated-constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct FailStruct { }
1919

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

2525
struct OKStruct2 { }
+3-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
error[E0308]: const not compatible with trait
2-
--> $DIR/trait-associated-constant.rs:21:5
3-
|
4-
LL | const AC: Option<&'c str> = None;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
6-
|
7-
= note: expected enum `Option<&'b str>`
8-
found enum `Option<&'c str>`
9-
note: the lifetime `'c` as defined here...
1+
error[E0207]: the lifetime parameter `'c` is not constrained by the impl trait, self type, or predicates
102
--> $DIR/trait-associated-constant.rs:20:18
113
|
124
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
13-
| ^^
14-
note: ...does not necessarily outlive the lifetime `'b` as defined here
15-
--> $DIR/trait-associated-constant.rs:20:14
16-
|
17-
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
18-
| ^^
5+
| ^^ unconstrained lifetime parameter
196

207
error: aborting due to previous error
218

22-
For more information about this error, try `rustc --explain E0308`.
9+
For more information about this error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)