From ecdcb9d6c8808c8206dbefc08b9f7542393686ca Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 7 Jan 2016 13:51:16 -0500 Subject: [PATCH 1/6] Change error scheme so that if projection fails we generate `A::B` instead of `TyError` Conflicts: src/test/compile-fail/issue-29857.rs --- src/librustc/middle/traits/project.rs | 25 ++++++++++++--- src/librustc_typeck/check/mod.rs | 3 ++ .../coherence-projection-conflict-ty-param.rs | 22 +++++++++++++ .../compile-fail/const-eval-overflow-4b.rs | 2 +- src/test/compile-fail/issue-19692.rs | 2 +- src/test/compile-fail/issue-21950.rs | 2 +- src/test/compile-fail/issue-23966.rs | 4 ++- src/test/compile-fail/issue-24352.rs | 2 +- src/test/compile-fail/issue-29857.rs | 31 +++++++++++++++++++ 9 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 src/test/compile-fail/coherence-projection-conflict-ty-param.rs create mode 100644 src/test/compile-fail/issue-29857.rs diff --git a/src/librustc/middle/traits/project.rs b/src/librustc/middle/traits/project.rs index 8fbfd513c78d6..28910ab1a044b 100644 --- a/src/librustc/middle/traits/project.rs +++ b/src/librustc/middle/traits/project.rs @@ -426,11 +426,25 @@ fn opt_normalize_projection_type<'a,'b,'tcx>( } } -/// in various error cases, we just set TyError and return an obligation -/// that, when fulfilled, will lead to an error. +/// If we are projecting `::Item`, but `T: Trait` does not +/// hold. In various error cases, we cannot generate a valid +/// normalized projection. Therefore, we create an inference variable +/// return an associated obligation that, when fulfilled, will lead to +/// an error. /// -/// FIXME: the TyError created here can enter the obligation we create, -/// leading to error messages involving TyError. +/// Note that we used to return `TyError` here, but that was quite +/// dubious -- the premise was that an error would *eventually* be +/// reported, when the obligation was processed. But in general once +/// you see a `TyError` you are supposed to be able to assume that an +/// error *has been* reported, so that you can take whatever heuristic +/// paths you want to take. To make things worse, it was possible for +/// cycles to arise, where you basically had a setup like ` +/// as Trait>::Foo == $0`. Here, normalizing ` as +/// Trait>::Foo> to `[type error]` would lead to an obligation of +/// ` as Trait>::Foo`. We are supposed to report +/// an error for this obligation, but we legitimately should not, +/// because it contains `[type error]`. Yuck! (See issue #29857 for +/// one case where this arose.) fn normalize_to_error<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>, projection_ty: ty::ProjectionTy<'tcx>, cause: ObligationCause<'tcx>, @@ -441,8 +455,9 @@ fn normalize_to_error<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>, let trait_obligation = Obligation { cause: cause, recursion_depth: depth, predicate: trait_ref.to_predicate() }; + let new_value = selcx.infcx().next_ty_var(); Normalized { - value: selcx.tcx().types.err, + value: new_value, obligations: vec!(trait_obligation) } } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index a50213202b82c..a1837fa38126a 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1059,6 +1059,9 @@ fn report_cast_to_unsized_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, t_cast: Ty<'tcx>, t_expr: Ty<'tcx>, id: ast::NodeId) { + if t_cast.references_error() || t_expr.references_error() { + return; + } let tstr = fcx.infcx().ty_to_string(t_cast); fcx.type_error_message(span, |actual| { format!("cast to unsized type: `{}` as `{}`", actual, tstr) diff --git a/src/test/compile-fail/coherence-projection-conflict-ty-param.rs b/src/test/compile-fail/coherence-projection-conflict-ty-param.rs new file mode 100644 index 0000000000000..6880f3e9a3cc9 --- /dev/null +++ b/src/test/compile-fail/coherence-projection-conflict-ty-param.rs @@ -0,0 +1,22 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Coherence error results because we do not know whether `T: Foo

` or not +// for the second impl. + +use std::marker::PhantomData; + +pub trait Foo

{} + +impl > Foo

for Option {} //~ ERROR E0119 + +impl Foo for Option { } + +fn main() {} diff --git a/src/test/compile-fail/const-eval-overflow-4b.rs b/src/test/compile-fail/const-eval-overflow-4b.rs index a8f47ab92e529..253285d3919c2 100644 --- a/src/test/compile-fail/const-eval-overflow-4b.rs +++ b/src/test/compile-fail/const-eval-overflow-4b.rs @@ -22,7 +22,7 @@ use std::{u8, u16, u32, u64, usize}; const A_I8_T : [u32; (i8::MAX as i8 + 1u8) as usize] //~^ ERROR mismatched types - //~| the trait `core::ops::Add` is not implemented for the type `i8` + //~| ERROR the trait `core::ops::Add` is not implemented for the type `i8` = [0; (i8::MAX as usize) + 1]; fn main() { diff --git a/src/test/compile-fail/issue-19692.rs b/src/test/compile-fail/issue-19692.rs index 88ae0f835d0d7..ca1715445e526 100644 --- a/src/test/compile-fail/issue-19692.rs +++ b/src/test/compile-fail/issue-19692.rs @@ -12,7 +12,7 @@ struct Homura; fn akemi(homura: Homura) { let Some(ref madoka) = Some(homura.kaname()); //~ ERROR no method named `kaname` found - madoka.clone(); //~ ERROR the type of this value must be known in this context + madoka.clone(); } fn main() { } diff --git a/src/test/compile-fail/issue-21950.rs b/src/test/compile-fail/issue-21950.rs index 900ad5ce812e3..ef6ce5c995bf3 100644 --- a/src/test/compile-fail/issue-21950.rs +++ b/src/test/compile-fail/issue-21950.rs @@ -16,5 +16,5 @@ fn main() { let x = &10 as &Add; //~^ ERROR the type parameter `RHS` must be explicitly specified in an object type because its default value `Self` references the type `Self` - //~^^ ERROR the value of the associated type `Output` (from the trait `core::ops::Add`) must be specified + //~| ERROR the value of the associated type `Output` (from the trait `core::ops::Add`) must be specified } diff --git a/src/test/compile-fail/issue-23966.rs b/src/test/compile-fail/issue-23966.rs index d7f909e4ebcf6..7f9c7a292f2be 100644 --- a/src/test/compile-fail/issue-23966.rs +++ b/src/test/compile-fail/issue-23966.rs @@ -9,5 +9,7 @@ // except according to those terms. fn main() { - "".chars().fold(|_, _| (), ()); //~ ERROR is not implemented for the type `()` + "".chars().fold(|_, _| (), ()); + //~^ ERROR E0277 + //~| ERROR E0277 } diff --git a/src/test/compile-fail/issue-24352.rs b/src/test/compile-fail/issue-24352.rs index 9936f67b3af3c..4b0773140566c 100644 --- a/src/test/compile-fail/issue-24352.rs +++ b/src/test/compile-fail/issue-24352.rs @@ -10,5 +10,5 @@ fn main() { 1.0f64 - 1.0; - 1.0f64 - 1 //~ ERROR: is not implemented + 1.0f64 - 1 //~ ERROR E0277 } diff --git a/src/test/compile-fail/issue-29857.rs b/src/test/compile-fail/issue-29857.rs new file mode 100644 index 0000000000000..661579f52b684 --- /dev/null +++ b/src/test/compile-fail/issue-29857.rs @@ -0,0 +1,31 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +#![feature(rustc_attrs)] + +use std::marker::PhantomData; + +pub trait Foo

{} + +impl > Foo

for Option {} + +pub struct Qux (PhantomData<*mut T>); + +impl Foo<*mut T> for Option> {} + +pub trait Bar { + type Output: 'static; +} + +impl> Foo<*mut T> for W {} + +#[rustc_error] +fn main() {} //~ ERROR compilation successful From f736290dcb60463d8f367b1b079cb13d051c1d65 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 25 Nov 2015 18:17:16 +0200 Subject: [PATCH 2/6] improve cast handling - this fixes test failures the problem is that now "type_is_known_to_be_sized" now returns false when called on a type with ty_err inside - this prevents spurious errors (we may want to move the check to check::cast anyway - see #12894). --- src/librustc_typeck/check/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index a1837fa38126a..e87af3a5dfbcb 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3510,9 +3510,10 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, let t_cast = structurally_resolved_type(fcx, expr.span, t_cast); check_expr_with_expectation(fcx, e, ExpectCastableToType(t_cast)); let t_expr = fcx.expr_ty(e); + let t_cast = fcx.infcx().resolve_type_vars_if_possible(&t_cast); // Eagerly check for some obvious errors. - if t_expr.references_error() { + if t_expr.references_error() || t_cast.references_error() { fcx.write_error(id); } else if !fcx.type_is_known_to_be_sized(t_cast, expr.span) { report_cast_to_unsized_type(fcx, expr.span, t.span, e.span, t_cast, t_expr, id); From d8e3ba41fc17c9c8f290d8f3e87969cfc01e6723 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 8 Jan 2016 20:16:30 -0500 Subject: [PATCH 3/6] permit coercions if `[error]` is found in either type Conflicts: src/test/compile-fail/issue-3973.rs --- src/librustc_typeck/check/coercion.rs | 55 +++++++++++++-------------- src/test/compile-fail/issue-19692.rs | 2 +- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 85f0aa3bbd3c3..f91f13f586c41 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -68,7 +68,7 @@ use middle::traits::{predicate_for_trait_def, report_selection_error}; use middle::ty::adjustment::{AutoAdjustment, AutoDerefRef, AdjustDerefRef}; use middle::ty::adjustment::{AutoPtr, AutoUnsafe, AdjustReifyFnPointer}; use middle::ty::adjustment::{AdjustUnsafeFnPointer}; -use middle::ty::{self, LvaluePreference, TypeAndMut, Ty}; +use middle::ty::{self, LvaluePreference, TypeAndMut, Ty, HasTypeFlags}; use middle::ty::error::TypeError; use middle::ty::relate::RelateResult; use util::common::indent; @@ -110,10 +110,15 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { a, b); + let a = self.fcx.infcx().shallow_resolve(a); + + // Just ignore error types. + if a.references_error() || b.references_error() { + return Ok(None); + } + // Consider coercing the subtype to a DST - let unsize = self.unpack_actual_value(a, |a| { - self.coerce_unsized(a, b) - }); + let unsize = self.coerce_unsized(a, b); if unsize.is_ok() { return unsize; } @@ -124,39 +129,33 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { // See above for details. match b.sty { ty::TyRawPtr(mt_b) => { - return self.unpack_actual_value(a, |a| { - self.coerce_unsafe_ptr(a, b, mt_b.mutbl) - }); + return self.coerce_unsafe_ptr(a, b, mt_b.mutbl); } ty::TyRef(_, mt_b) => { - return self.unpack_actual_value(a, |a| { - self.coerce_borrowed_pointer(expr_a, a, b, mt_b.mutbl) - }); + return self.coerce_borrowed_pointer(expr_a, a, b, mt_b.mutbl); } _ => {} } - self.unpack_actual_value(a, |a| { - match a.sty { - ty::TyBareFn(Some(_), a_f) => { - // Function items are coercible to any closure - // type; function pointers are not (that would - // require double indirection). - self.coerce_from_fn_item(a, a_f, b) - } - ty::TyBareFn(None, a_f) => { - // We permit coercion of fn pointers to drop the - // unsafe qualifier. - self.coerce_from_fn_pointer(a, a_f, b) - } - _ => { - // Otherwise, just use subtyping rules. - self.subtype(a, b) - } + match a.sty { + ty::TyBareFn(Some(_), a_f) => { + // Function items are coercible to any closure + // type; function pointers are not (that would + // require double indirection). + self.coerce_from_fn_item(a, a_f, b) } - }) + ty::TyBareFn(None, a_f) => { + // We permit coercion of fn pointers to drop the + // unsafe qualifier. + self.coerce_from_fn_pointer(a, a_f, b) + } + _ => { + // Otherwise, just use subtyping rules. + self.subtype(a, b) + } + } } /// Reborrows `&mut A` to `&mut B` and `&(mut) A` to `&B`. diff --git a/src/test/compile-fail/issue-19692.rs b/src/test/compile-fail/issue-19692.rs index ca1715445e526..53ad241687894 100644 --- a/src/test/compile-fail/issue-19692.rs +++ b/src/test/compile-fail/issue-19692.rs @@ -12,7 +12,7 @@ struct Homura; fn akemi(homura: Homura) { let Some(ref madoka) = Some(homura.kaname()); //~ ERROR no method named `kaname` found - madoka.clone(); + madoka.clone(); //~ ERROR the type of this value must be known } fn main() { } From 473c8f4eaee709a402465183464b191874830783 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 8 Jan 2016 21:00:24 -0500 Subject: [PATCH 4/6] Minor rebase corrections --- src/librustc_typeck/check/coercion.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index f91f13f586c41..8f64e85de4b0f 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -68,7 +68,8 @@ use middle::traits::{predicate_for_trait_def, report_selection_error}; use middle::ty::adjustment::{AutoAdjustment, AutoDerefRef, AdjustDerefRef}; use middle::ty::adjustment::{AutoPtr, AutoUnsafe, AdjustReifyFnPointer}; use middle::ty::adjustment::{AdjustUnsafeFnPointer}; -use middle::ty::{self, LvaluePreference, TypeAndMut, Ty, HasTypeFlags}; +use middle::ty::{self, LvaluePreference, TypeAndMut, Ty}; +use middle::ty::fold::TypeFoldable; use middle::ty::error::TypeError; use middle::ty::relate::RelateResult; use util::common::indent; From 06fc8bba8ca18db8323ea63358beb4dcb97095c0 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 11 Jan 2016 21:59:28 -0500 Subject: [PATCH 5/6] Adjust trait imports --- src/librustc_typeck/check/coercion.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 8f64e85de4b0f..ace731c00723c 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -68,8 +68,7 @@ use middle::traits::{predicate_for_trait_def, report_selection_error}; use middle::ty::adjustment::{AutoAdjustment, AutoDerefRef, AdjustDerefRef}; use middle::ty::adjustment::{AutoPtr, AutoUnsafe, AdjustReifyFnPointer}; use middle::ty::adjustment::{AdjustUnsafeFnPointer}; -use middle::ty::{self, LvaluePreference, TypeAndMut, Ty}; -use middle::ty::fold::TypeFoldable; +use middle::ty::{self, HasTypeFlags, LvaluePreference, TypeAndMut, Ty}; use middle::ty::error::TypeError; use middle::ty::relate::RelateResult; use util::common::indent; From e5954cd6be71425c82c96fdda6f7ce6134779cf9 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 12 Jan 2016 04:51:45 -0500 Subject: [PATCH 6/6] Update test -- here we get a RFC 1214 warning (that will eventually be an error) but the bugfix causes this warning to propagate and become an unresolved variable instead of a `TyError`, so we wind up seeing an obscure inference error afterwards, which is somewhat unfortunate, and suggests this bugfix can cause RFC 1214 warnings to become errors. --- src/test/run-pass/issue-25810.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/run-pass/issue-25810.rs b/src/test/run-pass/issue-25810.rs index 49a0642ce184d..820872ad3fc50 100644 --- a/src/test/run-pass/issue-25810.rs +++ b/src/test/run-pass/issue-25810.rs @@ -14,7 +14,9 @@ fn main() { println!("{:?}",y); } -trait Foo { +trait Foo + where for<'a> &'a Self: Bar +{ fn foo<'a>(&'a self) -> <&'a Self as Bar>::Output; }