Skip to content

Commit 8b1742e

Browse files
committed
Fix #54822 and associated faulty tests
Type checking associated constants can require trait bounds, but an empty parameter environment was provided to the trait solver. Providing an appropriate parameter environment seems to fix #54822 and also make one of the cases in src/test/ui/nll/trait-associated-constant.rs that should compile successfully do so. It also (slightly) improves the error message in src/test/ui/associated-const/associated-const-generic-obligations.rs
1 parent 9d71ec1 commit 8b1742e

6 files changed

+33
-28
lines changed

src/librustc_typeck/check/compare_method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
917917
debug!("compare_const_impl(impl_trait_ref={:?})", impl_trait_ref);
918918

919919
tcx.infer_ctxt().enter(|infcx| {
920-
let param_env = ty::ParamEnv::empty();
920+
let param_env = tcx.param_env(impl_c.def_id);
921921
let inh = Inherited::new(infcx, impl_c.def_id);
922922
let infcx = &inh.infcx;
923923

src/test/ui/associated-const/associated-const-generic-obligations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait Bar: Foo {
1212

1313
impl<T: Foo> Bar for T {
1414
const FROM: &'static str = "foo";
15-
//~^ ERROR the trait bound `T: Foo` is not satisfied [E0277]
15+
//~^ ERROR implemented const `FROM` has an incompatible type for trait [E0326]
1616
}
1717

1818
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
error[E0277]: the trait bound `T: Foo` is not satisfied
2-
--> $DIR/associated-const-generic-obligations.rs:14:5
1+
error[E0326]: implemented const `FROM` has an incompatible type for trait
2+
--> $DIR/associated-const-generic-obligations.rs:14:17
33
|
4+
LL | const FROM: Self::Out;
5+
| --------- type in trait
6+
...
47
LL | const FROM: &'static str = "foo";
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `T`
8+
| ^^^^^^^^^^^^ expected associated type, found reference
69
|
7-
= help: consider adding a `where T: Foo` bound
10+
= note: expected type `<T as Foo>::Out`
11+
found type `&'static str`
812

913
error: aborting due to previous error
1014

11-
For more information about this error, try `rustc --explain E0277`.
15+
For more information about this error, try `rustc --explain E0326`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile-pass
2+
3+
trait ConstDefault {
4+
const DEFAULT: Self;
5+
}
6+
7+
trait Foo: Sized {}
8+
9+
trait FooExt: Foo {
10+
type T: ConstDefault;
11+
}
12+
13+
trait Bar<F: FooExt> {
14+
const T: F::T;
15+
}
16+
17+
impl<F: FooExt> Bar<F> for () {
18+
const T: F::T = <F::T as ConstDefault>::DEFAULT;
19+
}
20+
21+
fn main() {}

src/test/ui/nll/trait-associated-constant.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ struct FailStruct2 { }
2626

2727
impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
2828
const AC: Option<&'a str> = None;
29-
//~^ ERROR: mismatched types
3029
}
3130

3231
fn main() {}

src/test/ui/nll/trait-associated-constant.stderr

+1-20
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,6 @@ note: ...does not necessarily outlive the lifetime 'b as defined on the impl at
1717
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
1818
| ^^
1919

20-
error[E0308]: mismatched types
21-
--> $DIR/trait-associated-constant.rs:28:5
22-
|
23-
LL | const AC: Option<&'a str> = None;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
25-
|
26-
= note: expected type `std::option::Option<&'b str>`
27-
found type `std::option::Option<&'a str>`
28-
note: the lifetime 'a as defined on the impl at 27:6...
29-
--> $DIR/trait-associated-constant.rs:27:6
30-
|
31-
LL | impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
32-
| ^^
33-
note: ...does not necessarily outlive the lifetime 'b as defined on the impl at 27:14
34-
--> $DIR/trait-associated-constant.rs:27:14
35-
|
36-
LL | impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
37-
| ^^
38-
39-
error: aborting due to 2 previous errors
20+
error: aborting due to previous error
4021

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

0 commit comments

Comments
 (0)