Skip to content

Commit c0dcfed

Browse files
authoredMar 13, 2019
Rollup merge of rust-lang#59083 - kyren:master, r=varkor
Fix rust-lang#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 rust-lang#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
2 parents 13a8942 + aa9bd68 commit c0dcfed

6 files changed

+41
-36
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

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@ trait Anything<'a: 'b, 'b> {
99
const AC: Option<&'b str>;
1010
}
1111

12-
struct OKStruct { }
12+
struct OKStruct1 { }
1313

14-
impl<'a: 'b, 'b> Anything<'a, 'b> for OKStruct {
14+
impl<'a: 'b, 'b> Anything<'a, 'b> for OKStruct1 {
1515
const AC: Option<&'b str> = None;
1616
}
1717

18-
struct FailStruct1 { }
18+
struct FailStruct { }
1919

20-
impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
20+
impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
2121
const AC: Option<&'c str> = None;
2222
//~^ ERROR: mismatched types
2323
}
2424

25-
struct FailStruct2 { }
25+
struct OKStruct2 { }
2626

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

3231
fn main() {}

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

+3-22
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,14 @@ LL | const AC: Option<&'c str> = None;
99
note: the lifetime 'c as defined on the impl at 20:18...
1010
--> $DIR/trait-associated-constant.rs:20:18
1111
|
12-
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
12+
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
1313
| ^^
1414
note: ...does not necessarily outlive the lifetime 'b as defined on the impl at 20:14
1515
--> $DIR/trait-associated-constant.rs:20:14
1616
|
17-
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct1 {
17+
LL | impl<'a: 'b, 'b, 'c> Anything<'a, 'b> for FailStruct {
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)
Please sign in to comment.