Skip to content

Commit cedaee6

Browse files
committed
Merge pull request #30847 from nikomatsakis/issue-29857-beta
[beta] Backport fix for issue 29857
2 parents 4dd6b09 + e5954cd commit cedaee6

File tree

11 files changed

+115
-40
lines changed

11 files changed

+115
-40
lines changed

src/librustc/middle/traits/project.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,25 @@ fn opt_normalize_projection_type<'a,'b,'tcx>(
426426
}
427427
}
428428

429-
/// in various error cases, we just set TyError and return an obligation
430-
/// that, when fulfilled, will lead to an error.
429+
/// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
430+
/// hold. In various error cases, we cannot generate a valid
431+
/// normalized projection. Therefore, we create an inference variable
432+
/// return an associated obligation that, when fulfilled, will lead to
433+
/// an error.
431434
///
432-
/// FIXME: the TyError created here can enter the obligation we create,
433-
/// leading to error messages involving TyError.
435+
/// Note that we used to return `TyError` here, but that was quite
436+
/// dubious -- the premise was that an error would *eventually* be
437+
/// reported, when the obligation was processed. But in general once
438+
/// you see a `TyError` you are supposed to be able to assume that an
439+
/// error *has been* reported, so that you can take whatever heuristic
440+
/// paths you want to take. To make things worse, it was possible for
441+
/// cycles to arise, where you basically had a setup like `<MyType<$0>
442+
/// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
443+
/// Trait>::Foo> to `[type error]` would lead to an obligation of
444+
/// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
445+
/// an error for this obligation, but we legitimately should not,
446+
/// because it contains `[type error]`. Yuck! (See issue #29857 for
447+
/// one case where this arose.)
434448
fn normalize_to_error<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>,
435449
projection_ty: ty::ProjectionTy<'tcx>,
436450
cause: ObligationCause<'tcx>,
@@ -441,8 +455,9 @@ fn normalize_to_error<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>,
441455
let trait_obligation = Obligation { cause: cause,
442456
recursion_depth: depth,
443457
predicate: trait_ref.to_predicate() };
458+
let new_value = selcx.infcx().next_ty_var();
444459
Normalized {
445-
value: selcx.tcx().types.err,
460+
value: new_value,
446461
obligations: vec!(trait_obligation)
447462
}
448463
}

src/librustc_typeck/check/coercion.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use middle::traits::{predicate_for_trait_def, report_selection_error};
6868
use middle::ty::adjustment::{AutoAdjustment, AutoDerefRef, AdjustDerefRef};
6969
use middle::ty::adjustment::{AutoPtr, AutoUnsafe, AdjustReifyFnPointer};
7070
use middle::ty::adjustment::{AdjustUnsafeFnPointer};
71-
use middle::ty::{self, LvaluePreference, TypeAndMut, Ty};
71+
use middle::ty::{self, HasTypeFlags, LvaluePreference, TypeAndMut, Ty};
7272
use middle::ty::error::TypeError;
7373
use middle::ty::relate::RelateResult;
7474
use util::common::indent;
@@ -110,10 +110,15 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
110110
a,
111111
b);
112112

113+
let a = self.fcx.infcx().shallow_resolve(a);
114+
115+
// Just ignore error types.
116+
if a.references_error() || b.references_error() {
117+
return Ok(None);
118+
}
119+
113120
// Consider coercing the subtype to a DST
114-
let unsize = self.unpack_actual_value(a, |a| {
115-
self.coerce_unsized(a, b)
116-
});
121+
let unsize = self.coerce_unsized(a, b);
117122
if unsize.is_ok() {
118123
return unsize;
119124
}
@@ -124,39 +129,33 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
124129
// See above for details.
125130
match b.sty {
126131
ty::TyRawPtr(mt_b) => {
127-
return self.unpack_actual_value(a, |a| {
128-
self.coerce_unsafe_ptr(a, b, mt_b.mutbl)
129-
});
132+
return self.coerce_unsafe_ptr(a, b, mt_b.mutbl);
130133
}
131134

132135
ty::TyRef(_, mt_b) => {
133-
return self.unpack_actual_value(a, |a| {
134-
self.coerce_borrowed_pointer(expr_a, a, b, mt_b.mutbl)
135-
});
136+
return self.coerce_borrowed_pointer(expr_a, a, b, mt_b.mutbl);
136137
}
137138

138139
_ => {}
139140
}
140141

141-
self.unpack_actual_value(a, |a| {
142-
match a.sty {
143-
ty::TyBareFn(Some(_), a_f) => {
144-
// Function items are coercible to any closure
145-
// type; function pointers are not (that would
146-
// require double indirection).
147-
self.coerce_from_fn_item(a, a_f, b)
148-
}
149-
ty::TyBareFn(None, a_f) => {
150-
// We permit coercion of fn pointers to drop the
151-
// unsafe qualifier.
152-
self.coerce_from_fn_pointer(a, a_f, b)
153-
}
154-
_ => {
155-
// Otherwise, just use subtyping rules.
156-
self.subtype(a, b)
157-
}
142+
match a.sty {
143+
ty::TyBareFn(Some(_), a_f) => {
144+
// Function items are coercible to any closure
145+
// type; function pointers are not (that would
146+
// require double indirection).
147+
self.coerce_from_fn_item(a, a_f, b)
158148
}
159-
})
149+
ty::TyBareFn(None, a_f) => {
150+
// We permit coercion of fn pointers to drop the
151+
// unsafe qualifier.
152+
self.coerce_from_fn_pointer(a, a_f, b)
153+
}
154+
_ => {
155+
// Otherwise, just use subtyping rules.
156+
self.subtype(a, b)
157+
}
158+
}
160159
}
161160

162161
/// Reborrows `&mut A` to `&mut B` and `&(mut) A` to `&B`.

src/librustc_typeck/check/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,9 @@ fn report_cast_to_unsized_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
10591059
t_cast: Ty<'tcx>,
10601060
t_expr: Ty<'tcx>,
10611061
id: ast::NodeId) {
1062+
if t_cast.references_error() || t_expr.references_error() {
1063+
return;
1064+
}
10621065
let tstr = fcx.infcx().ty_to_string(t_cast);
10631066
fcx.type_error_message(span, |actual| {
10641067
format!("cast to unsized type: `{}` as `{}`", actual, tstr)
@@ -3507,9 +3510,10 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35073510
let t_cast = structurally_resolved_type(fcx, expr.span, t_cast);
35083511
check_expr_with_expectation(fcx, e, ExpectCastableToType(t_cast));
35093512
let t_expr = fcx.expr_ty(e);
3513+
let t_cast = fcx.infcx().resolve_type_vars_if_possible(&t_cast);
35103514

35113515
// Eagerly check for some obvious errors.
3512-
if t_expr.references_error() {
3516+
if t_expr.references_error() || t_cast.references_error() {
35133517
fcx.write_error(id);
35143518
} else if !fcx.type_is_known_to_be_sized(t_cast, expr.span) {
35153519
report_cast_to_unsized_type(fcx, expr.span, t.span, e.span, t_cast, t_expr, id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Coherence error results because we do not know whether `T: Foo<P>` or not
12+
// for the second impl.
13+
14+
use std::marker::PhantomData;
15+
16+
pub trait Foo<P> {}
17+
18+
impl <P, T: Foo<P>> Foo<P> for Option<T> {} //~ ERROR E0119
19+
20+
impl<T, U> Foo<T> for Option<U> { }
21+
22+
fn main() {}

src/test/compile-fail/const-eval-overflow-4b.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::{u8, u16, u32, u64, usize};
2222
const A_I8_T
2323
: [u32; (i8::MAX as i8 + 1u8) as usize]
2424
//~^ ERROR mismatched types
25-
//~| the trait `core::ops::Add<u8>` is not implemented for the type `i8`
25+
//~| ERROR the trait `core::ops::Add<u8>` is not implemented for the type `i8`
2626
= [0; (i8::MAX as usize) + 1];
2727

2828
fn main() {

src/test/compile-fail/issue-19692.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Homura;
1212

1313
fn akemi(homura: Homura) {
1414
let Some(ref madoka) = Some(homura.kaname()); //~ ERROR no method named `kaname` found
15-
madoka.clone(); //~ ERROR the type of this value must be known in this context
15+
madoka.clone(); //~ ERROR the type of this value must be known
1616
}
1717

1818
fn main() { }

src/test/compile-fail/issue-21950.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ fn main() {
1616
let x = &10 as
1717
&Add;
1818
//~^ ERROR the type parameter `RHS` must be explicitly specified in an object type because its default value `Self` references the type `Self`
19-
//~^^ ERROR the value of the associated type `Output` (from the trait `core::ops::Add`) must be specified
19+
//~| ERROR the value of the associated type `Output` (from the trait `core::ops::Add`) must be specified
2020
}

src/test/compile-fail/issue-23966.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
"".chars().fold(|_, _| (), ()); //~ ERROR is not implemented for the type `()`
12+
"".chars().fold(|_, _| (), ());
13+
//~^ ERROR E0277
14+
//~| ERROR E0277
1315
}

src/test/compile-fail/issue-24352.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
fn main() {
1212
1.0f64 - 1.0;
13-
1.0f64 - 1 //~ ERROR: is not implemented
13+
1.0f64 - 1 //~ ERROR E0277
1414
}

src/test/compile-fail/issue-29857.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
#![feature(rustc_attrs)]
13+
14+
use std::marker::PhantomData;
15+
16+
pub trait Foo<P> {}
17+
18+
impl <P, T: Foo<P>> Foo<P> for Option<T> {}
19+
20+
pub struct Qux<T> (PhantomData<*mut T>);
21+
22+
impl<T> Foo<*mut T> for Option<Qux<T>> {}
23+
24+
pub trait Bar {
25+
type Output: 'static;
26+
}
27+
28+
impl<T: 'static, W: Bar<Output = T>> Foo<*mut T> for W {}
29+
30+
#[rustc_error]
31+
fn main() {} //~ ERROR compilation successful

src/test/run-pass/issue-25810.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ fn main() {
1414
println!("{:?}",y);
1515
}
1616

17-
trait Foo {
17+
trait Foo
18+
where for<'a> &'a Self: Bar
19+
{
1820
fn foo<'a>(&'a self) -> <&'a Self as Bar>::Output;
1921
}
2022

0 commit comments

Comments
 (0)