From 32d0463f89c6be5e8da6081363edc2567fc4b63f Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Sat, 29 Feb 2020 10:03:04 +1300 Subject: [PATCH 01/17] Assume unevaluated consts are equal to the other consts and add ConstEquate obligation. This delays the need to evaluate consts eagerly and therefore gets around const eval query cycles. --- .../infer/canonical/query_response.rs | 6 +- src/librustc_infer/infer/combine.rs | 16 ++- src/librustc_infer/infer/equate.rs | 14 ++- src/librustc_infer/infer/glb.rs | 12 ++- src/librustc_infer/infer/lub.rs | 12 ++- src/librustc_infer/infer/mod.rs | 20 ++++ src/librustc_infer/infer/nll_relate/mod.rs | 15 ++- src/librustc_infer/infer/outlives/mod.rs | 3 +- src/librustc_infer/infer/sub.rs | 12 ++- src/librustc_infer/traits/mod.rs | 3 +- src/librustc_infer/traits/structural_impls.rs | 3 + src/librustc_infer/traits/util.rs | 6 ++ src/librustc_lint/builtin.rs | 3 +- src/librustc_middle/ty/mod.rs | 12 ++- src/librustc_middle/ty/outlives.rs | 6 +- src/librustc_middle/ty/print/pretty.rs | 7 ++ src/librustc_middle/ty/relate.rs | 37 +++---- src/librustc_middle/ty/structural_impls.rs | 4 + .../borrow_check/type_check/relate_tys.rs | 4 +- .../transform/qualify_min_const_fn.rs | 3 +- src/librustc_trait_selection/opaque_types.rs | 3 +- .../traits/error_reporting/mod.rs | 21 ++++ .../traits/fulfill.rs | 62 +++++++++++- .../traits/object_safety.rs | 98 +++++++++++-------- .../traits/project.rs | 5 - .../traits/query/normalize.rs | 5 - src/librustc_trait_selection/traits/select.rs | 35 +++++++ src/librustc_trait_selection/traits/wf.rs | 4 + .../implied_outlives_bounds.rs | 3 +- .../normalize_erasing_regions.rs | 3 +- src/librustc_typeck/check/method/probe.rs | 3 +- src/librustc_typeck/check/mod.rs | 10 ++ src/librustc_typeck/collect.rs | 30 +----- .../impl_wf_check/min_specialization.rs | 3 +- src/librustc_typeck/outlives/explicit.rs | 3 +- src/librustdoc/clean/mod.rs | 3 +- 36 files changed, 367 insertions(+), 122 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index 9fe7ebf58b343..49df21cd41b3b 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -25,7 +25,7 @@ use rustc_middle::arena::ArenaAllocatable; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::subst::{GenericArg, GenericArgKind}; -use rustc_middle::ty::{self, BoundVar, Ty, TyCtxt}; +use rustc_middle::ty::{self, BoundVar, Const, Ty, TyCtxt}; use std::fmt::Debug; impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { @@ -671,6 +671,10 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> { }); } + fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) { + unimplemented!() + } + fn normalization() -> NormalizationStrategy { NormalizationStrategy::Eager } diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index b03044b72da40..541d86ab9945d 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -164,7 +164,6 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { return self.unify_const_variable(!a_is_expected, vid, a); } - _ => {} } @@ -375,6 +374,20 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { debug!("generalize: success {{ {:?}, {:?} }}", ty, needs_wf); Ok(Generalization { ty, needs_wf }) } + + pub fn add_const_equate_obligation( + &mut self, + a_is_expected: bool, + a: &'tcx ty::Const<'tcx>, + b: &'tcx ty::Const<'tcx>, + ) { + let predicate = if a_is_expected { + ty::Predicate::ConstEquate(a, b) + } else { + ty::Predicate::ConstEquate(b, a) + }; + self.obligations.push(Obligation::new(self.trace.cause.clone(), self.param_env, predicate)); + } } struct Generalizer<'cx, 'tcx> { @@ -635,6 +648,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { } } } + ty::ConstKind::Unevaluated(..) => Ok(c), _ => relate::super_relate_consts(self, c, c), } } diff --git a/src/librustc_infer/infer/equate.rs b/src/librustc_infer/infer/equate.rs index e05094cda2762..160f746bdf7ef 100644 --- a/src/librustc_infer/infer/equate.rs +++ b/src/librustc_infer/infer/equate.rs @@ -4,7 +4,7 @@ use super::Subtype; use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation}; use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::TyVar; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, ConstKind, Ty, TyCtxt}; use rustc_hir::def_id::DefId; @@ -119,7 +119,17 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - self.fields.infcx.super_combine_consts(self, a, b) + match (a.val, b.val) { + (ConstKind::Unevaluated(..), _) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(b) + } + (_, ConstKind::Unevaluated(..)) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(a) + } + _ => self.fields.infcx.super_combine_consts(self, a, b), + } } fn binders( diff --git a/src/librustc_infer/infer/glb.rs b/src/librustc_infer/infer/glb.rs index f95d74a9340c9..583e80efc7c55 100644 --- a/src/librustc_infer/infer/glb.rs +++ b/src/librustc_infer/infer/glb.rs @@ -79,7 +79,17 @@ impl TypeRelation<'tcx> for Glb<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - self.fields.infcx.super_combine_consts(self, a, b) + match (a.val, b.val) { + (ty::ConstKind::Unevaluated(..), _) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(b) + } + (_, ty::ConstKind::Unevaluated(..)) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(a) + } + _ => self.fields.infcx.super_combine_consts(self, a, b), + } } fn binders( diff --git a/src/librustc_infer/infer/lub.rs b/src/librustc_infer/infer/lub.rs index 492f2b229d36d..e613bd5dc7a89 100644 --- a/src/librustc_infer/infer/lub.rs +++ b/src/librustc_infer/infer/lub.rs @@ -79,7 +79,17 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - self.fields.infcx.super_combine_consts(self, a, b) + match (a.val, b.val) { + (ty::ConstKind::Unevaluated(..), _) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(b) + } + (_, ty::ConstKind::Unevaluated(..)) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(a) + } + _ => self.fields.infcx.super_combine_consts(self, a, b), + } } fn binders( diff --git a/src/librustc_infer/infer/mod.rs b/src/librustc_infer/infer/mod.rs index 54f80e8f38812..9afce2629109a 100644 --- a/src/librustc_infer/infer/mod.rs +++ b/src/librustc_infer/infer/mod.rs @@ -1457,6 +1457,17 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { self.report_and_explain_type_error(trace, &err) } + pub fn report_mismatched_consts( + &self, + cause: &ObligationCause<'tcx>, + expected: &'tcx ty::Const<'tcx>, + actual: &'tcx ty::Const<'tcx>, + err: TypeError<'tcx>, + ) -> DiagnosticBuilder<'tcx> { + let trace = TypeTrace::consts(cause, true, expected, actual); + self.report_and_explain_type_error(trace, &err) + } + pub fn replace_bound_vars_with_fresh_vars( &self, span: Span, @@ -1743,6 +1754,15 @@ impl<'tcx> TypeTrace<'tcx> { TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) } } + pub fn consts( + cause: &ObligationCause<'tcx>, + a_is_expected: bool, + a: &'tcx ty::Const<'tcx>, + b: &'tcx ty::Const<'tcx>, + ) -> TypeTrace<'tcx> { + TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) } + } + pub fn dummy(tcx: TyCtxt<'tcx>) -> TypeTrace<'tcx> { TypeTrace { cause: ObligationCause::dummy(), diff --git a/src/librustc_infer/infer/nll_relate/mod.rs b/src/librustc_infer/infer/nll_relate/mod.rs index a2907e6e373b8..2787d7fef91a7 100644 --- a/src/librustc_infer/infer/nll_relate/mod.rs +++ b/src/librustc_infer/infer/nll_relate/mod.rs @@ -77,6 +77,8 @@ pub trait TypeRelatingDelegate<'tcx> { /// delegate. fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>); + fn const_equate(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>); + /// Creates a new universe index. Used when instantiating placeholders. fn create_next_universe(&mut self) -> ty::UniverseIndex; @@ -592,8 +594,16 @@ where b = self.infcx.shallow_resolve(b); } - match b.val { - ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { + match (a.val, b.val) { + (ty::ConstKind::Unevaluated(..), _) => { + self.delegate.const_equate(a, b); + Ok(b) + } + (_, ty::ConstKind::Unevaluated(..)) => { + self.delegate.const_equate(a, b); + Ok(a) + } + (_, ty::ConstKind::Infer(InferConst::Var(_))) if D::forbid_inference_vars() => { // Forbid inference variables in the RHS. bug!("unexpected inference var {:?}", b) } @@ -974,6 +984,7 @@ where } } } + ty::ConstKind::Unevaluated(..) => Ok(a), _ => relate::super_relate_consts(self, a, a), } } diff --git a/src/librustc_infer/infer/outlives/mod.rs b/src/librustc_infer/infer/outlives/mod.rs index ed3d860586968..289457e2bd0c2 100644 --- a/src/librustc_infer/infer/outlives/mod.rs +++ b/src/librustc_infer/infer/outlives/mod.rs @@ -19,7 +19,8 @@ pub fn explicit_outlives_bounds<'tcx>( | ty::Predicate::ObjectSafe(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::TypeOutlives(..) - | ty::Predicate::ConstEvaluatable(..) => None, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => None, ty::Predicate::RegionOutlives(ref data) => data .no_bound_vars() .map(|ty::OutlivesPredicate(r_a, r_b)| OutlivesBound::RegionSubRegion(r_b, r_a)), diff --git a/src/librustc_infer/infer/sub.rs b/src/librustc_infer/infer/sub.rs index 080af37492d89..a432fd7d1a130 100644 --- a/src/librustc_infer/infer/sub.rs +++ b/src/librustc_infer/infer/sub.rs @@ -155,7 +155,17 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - self.fields.infcx.super_combine_consts(self, a, b) + match (a.val, b.val) { + (ty::ConstKind::Unevaluated(..), _) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(b) + } + (_, ty::ConstKind::Unevaluated(..)) => { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + Ok(a) + } + _ => self.fields.infcx.super_combine_consts(self, a, b), + } } fn binders( diff --git a/src/librustc_infer/traits/mod.rs b/src/librustc_infer/traits/mod.rs index 2210c663d1469..078678f31c393 100644 --- a/src/librustc_infer/traits/mod.rs +++ b/src/librustc_infer/traits/mod.rs @@ -10,7 +10,7 @@ pub mod util; use rustc_hir as hir; use rustc_middle::ty::error::{ExpectedFound, TypeError}; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Const, Ty}; use rustc_span::Span; pub use self::FulfillmentErrorCode::*; @@ -80,6 +80,7 @@ pub enum FulfillmentErrorCode<'tcx> { CodeSelectionError(SelectionError<'tcx>), CodeProjectionError(MismatchedProjectionTypes<'tcx>), CodeSubtypeError(ExpectedFound>, TypeError<'tcx>), // always comes from a SubtypePredicate + CodeConstEquateError(ExpectedFound<&'tcx Const<'tcx>>, TypeError<'tcx>), CodeAmbiguity, } diff --git a/src/librustc_infer/traits/structural_impls.rs b/src/librustc_infer/traits/structural_impls.rs index 595a8cd768390..c48e58c04824e 100644 --- a/src/librustc_infer/traits/structural_impls.rs +++ b/src/librustc_infer/traits/structural_impls.rs @@ -41,6 +41,9 @@ impl<'tcx> fmt::Debug for traits::FulfillmentErrorCode<'tcx> { super::CodeSubtypeError(ref a, ref b) => { write!(f, "CodeSubtypeError({:?}, {:?})", a, b) } + super::CodeConstEquateError(ref a, ref b) => { + write!(f, "CodeConstEquateError({:?}, {:?})", a, b) + } super::CodeAmbiguity => write!(f, "Ambiguity"), } } diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 55bea57f3e243..da47de036f87d 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -42,6 +42,8 @@ pub fn anonymize_predicate<'tcx>( ty::Predicate::ConstEvaluatable(def_id, substs) => { ty::Predicate::ConstEvaluatable(def_id, substs) } + + ty::Predicate::ConstEquate(c1, c2) => ty::Predicate::ConstEquate(c1, c2), } } @@ -188,6 +190,10 @@ impl Elaborator<'tcx> { // Currently, we do not elaborate const-evaluatable // predicates. } + ty::Predicate::ConstEquate(..) => { + // Currently, we do not elaborate const-equate + // predicates. + } ty::Predicate::RegionOutlives(..) => { // Nothing to elaborate from `'a: 'b`. } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index cad6a312521e4..552db2f89bb33 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1221,7 +1221,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints { ObjectSafe(..) | ClosureKind(..) | Subtype(..) | - ConstEvaluatable(..) => continue, + ConstEvaluatable(..) | + ConstEquate(..) => continue, }; if predicate.is_global() { cx.struct_span_lint(TRIVIAL_BOUNDS, span, |lint| { diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 8bb9934789a23..360850c0fd9e5 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1193,6 +1193,9 @@ pub enum Predicate<'tcx> { /// Constant initializer must evaluate successfully. ConstEvaluatable(DefId, SubstsRef<'tcx>), + + /// Constants must be equal. The first component is the const that is expected. + ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), } /// The crate outlives map is computed during typeck and contains the @@ -1311,6 +1314,9 @@ impl<'tcx> Predicate<'tcx> { Predicate::ConstEvaluatable(def_id, const_substs) => { Predicate::ConstEvaluatable(def_id, const_substs.subst(tcx, substs)) } + Predicate::ConstEquate(c1, c2) => { + Predicate::ConstEquate(c1.subst(tcx, substs), c2.subst(tcx, substs)) + } } } } @@ -1488,7 +1494,8 @@ impl<'tcx> Predicate<'tcx> { | Predicate::ObjectSafe(..) | Predicate::ClosureKind(..) | Predicate::TypeOutlives(..) - | Predicate::ConstEvaluatable(..) => None, + | Predicate::ConstEvaluatable(..) + | Predicate::ConstEquate(..) => None, } } @@ -1502,7 +1509,8 @@ impl<'tcx> Predicate<'tcx> { | Predicate::WellFormed(..) | Predicate::ObjectSafe(..) | Predicate::ClosureKind(..) - | Predicate::ConstEvaluatable(..) => None, + | Predicate::ConstEvaluatable(..) + | Predicate::ConstEquate(..) => None, } } } diff --git a/src/librustc_middle/ty/outlives.rs b/src/librustc_middle/ty/outlives.rs index 4fd4d0d1f0627..32dfed5271445 100644 --- a/src/librustc_middle/ty/outlives.rs +++ b/src/librustc_middle/ty/outlives.rs @@ -83,6 +83,11 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo } } + ty::Array(element, _) => { + // Don't look into the len const as it doesn't affect regions + compute_components(tcx, element, out); + } + ty::Closure(_, ref substs) => { for upvar_ty in substs.as_closure().upvar_tys() { compute_components(tcx, upvar_ty, out); @@ -160,7 +165,6 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo ty::Opaque(..) | // OutlivesNominalType (ish) ty::Foreign(..) | // OutlivesNominalType ty::Str | // OutlivesScalar (ish) - ty::Array(..) | // ... ty::Slice(..) | // ... ty::RawPtr(..) | // ... ty::Ref(..) | // OutlivesReference diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 2d2704fc2bd89..1e3de4d31e5ed 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -2063,6 +2063,13 @@ define_print_and_forward_display! { print_value_path(def_id, substs), write("` can be evaluated")) } + ty::Predicate::ConstEquate(c1, c2) => { + p!(write("the constant `"), + print(c1), + write("` equals `"), + print(c2), + write("`")) + } } } diff --git a/src/librustc_middle/ty/relate.rs b/src/librustc_middle/ty/relate.rs index faa74f81e818b..225023618f73f 100644 --- a/src/librustc_middle/ty/relate.rs +++ b/src/librustc_middle/ty/relate.rs @@ -432,16 +432,17 @@ pub fn super_relate_tys>( match relation.relate(&sz_a, &sz_b) { Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))), Err(err) => { - // Check whether the lengths are both concrete/known values, - // but are unequal, for better diagnostics. - let sz_a = sz_a.try_eval_usize(tcx, relation.param_env()); - let sz_b = sz_b.try_eval_usize(tcx, relation.param_env()); - match (sz_a, sz_b) { - (Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize( - expected_found(relation, &sz_a_val, &sz_b_val), - )), - _ => Err(err), - } + // // Check whether the lengths are both concrete/known values, + // // but are unequal, for better diagnostics. + // let sz_a = sz_a.try_eval_usize(tcx, relation.param_env()); + // let sz_b = sz_b.try_eval_usize(tcx, relation.param_env()); + // match (sz_a, sz_b) { + // (Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize( + // expected_found(relation, &sz_a_val, &sz_b_val), + // )), + // _ => Err(err), + // } + Err(err) } } } @@ -610,14 +611,14 @@ pub fn super_relate_consts>( } // FIXME(const_generics): this is wrong, as it is a projection - ( - ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted), - ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted), - ) if a_def_id == b_def_id && a_promoted == b_promoted => { - let substs = - relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?; - Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted)) - } + // ( + // ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted), + // ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted), + // ) if a_def_id == b_def_id && a_promoted == b_promoted => { + // let substs = + // relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?; + // Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted)) + // } _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))), }; new_const_val.map(|val| tcx.mk_const(ty::Const { val, ty: a.ty })) diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs index c8406a024ecff..1458ea2cd7372 100644 --- a/src/librustc_middle/ty/structural_impls.rs +++ b/src/librustc_middle/ty/structural_impls.rs @@ -240,6 +240,7 @@ impl fmt::Debug for ty::Predicate<'tcx> { ty::Predicate::ConstEvaluatable(def_id, substs) => { write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs) } + ty::Predicate::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2), } } } @@ -492,6 +493,9 @@ impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> { ty::Predicate::ConstEvaluatable(def_id, substs) => { tcx.lift(&substs).map(|substs| ty::Predicate::ConstEvaluatable(def_id, substs)) } + ty::Predicate::ConstEquate(c1, c2) => { + tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::Predicate::ConstEquate(c1, c2)) + } } } } diff --git a/src/librustc_mir/borrow_check/type_check/relate_tys.rs b/src/librustc_mir/borrow_check/type_check/relate_tys.rs index 96ae534c3963f..c3b0bd82398cb 100644 --- a/src/librustc_mir/borrow_check/type_check/relate_tys.rs +++ b/src/librustc_mir/borrow_check/type_check/relate_tys.rs @@ -2,7 +2,7 @@ use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRe use rustc_infer::infer::{InferCtxt, NLLRegionVariableOrigin}; use rustc_middle::mir::ConstraintCategory; use rustc_middle::ty::relate::TypeRelation; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Const, Ty}; use rustc_trait_selection::traits::query::Fallible; use crate::borrow_check::constraints::OutlivesConstraint; @@ -99,6 +99,8 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> { } } + fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {} + fn normalization() -> NormalizationStrategy { NormalizationStrategy::Eager } diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index c9982aeaa0839..a2e2c1bf9eda3 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -28,7 +28,8 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - | Predicate::TypeOutlives(_) | Predicate::WellFormed(_) | Predicate::Projection(_) - | Predicate::ConstEvaluatable(..) => continue, + | Predicate::ConstEvaluatable(..) + | Predicate::ConstEquate(..) => continue, Predicate::ObjectSafe(_) => { bug!("object safe predicate on function: {:#?}", predicate) } diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 4f8075b0171d3..396965fcfb8b7 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1277,7 +1277,8 @@ crate fn required_region_bounds( | ty::Predicate::ObjectSafe(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::RegionOutlives(..) - | ty::Predicate::ConstEvaluatable(..) => None, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => None, ty::Predicate::TypeOutlives(predicate) => { // Search for a bound of the form `erased_self_ty // : 'a`, but be wary of something like `for<'a> diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index fa2af24c94534..1737d505367ec 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -589,6 +589,18 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { obligation ) } + + + ty::Predicate::ConstEquate(..) => { + // Errors for `ConstEquate` predicates show up as + // `SelectionError::ConstEvalFailure`, + // not `Unimplemented`. + span_bug!( + span, + "const-equate requirement gave wrong error: `{:?}`", + obligation + ) + } } } @@ -1066,6 +1078,15 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { ) .emit(); } + FulfillmentErrorCode::CodeConstEquateError(ref expected_found, ref err) => { + self.report_mismatched_consts( + &error.obligation.cause, + expected_found.expected, + expected_found.found, + err.clone(), + ) + .emit(); + } } } diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index 300acf95c9973..0c3b3252c66fe 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -3,8 +3,9 @@ use rustc_data_structures::obligation_forest::ProcessResult; use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation}; use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor}; use rustc_infer::traits::{TraitEngine, TraitEngineExt as _}; +use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::error::ExpectedFound; -use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeFoldable}; +use rustc_middle::ty::{self, Const, ToPolyTraitRef, Ty, TypeFoldable}; use std::marker::PhantomData; use super::project; @@ -512,6 +513,65 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))), } } + + ty::Predicate::ConstEquate(c1, c2) => { + debug!("equating consts: c1={:?} c2={:?}", c1, c2); + + let stalled_on = &mut pending_obligation.stalled_on; + + let mut evaluate = |c: &'tcx Const<'tcx>| { + if let ty::ConstKind::Unevaluated(def_id, substs, promoted) = c.val { + match self.selcx.infcx().const_eval_resolve( + obligation.param_env, + def_id, + substs, + promoted, + Some(obligation.cause.span), + ) { + Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)), + Err(ErrorHandled::TooGeneric) => { + stalled_on.append( + &mut substs.types().filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty)).collect(), + ); + Err(ProcessResult::Unchanged) + } + Err(err) => { + Err(ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err)))) + } + } + } else { + Ok(c) + } + }; + + match (evaluate(c1), evaluate(c2)) { + (Ok(c1), Ok(c2)) => { + match self + .selcx + .infcx() + .at(&obligation.cause, obligation.param_env) + .eq(c1, c2) + { + Ok(_) => ProcessResult::Changed(vec![]), + Err(err) => { + ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError( + ExpectedFound::new(true, c1, c2), + err, + )) + } + } + } + // FIXME(skinny121) How to report both errors if both produces errors? + (Err(result @ ProcessResult::Error(_)), _) + | (_, Err(result @ ProcessResult::Error(_))) => result, + (Err(ProcessResult::Unchanged), _) | (_, Err(ProcessResult::Unchanged)) => { + ProcessResult::Unchanged + } + _ => { + unreachable!("evaluate shouldn't itself return ProcessResult::Changed(..)") + } + } + } } } diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index 96b2b904e6543..3b4533b04ba4c 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -16,8 +16,8 @@ use crate::traits::{self, Obligation, ObligationCause}; use rustc_errors::{Applicability, FatalError}; use rustc_hir as hir; use rustc_hir::def_id::DefId; -use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst}; -use rustc_middle::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness}; +use rustc_middle::ty::subst::{GenericArg, InternalSubsts, Subst}; +use rustc_middle::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitor, WithConstness}; use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY; use rustc_span::symbol::Symbol; use rustc_span::Span; @@ -281,7 +281,8 @@ fn predicates_reference_self( | ty::Predicate::RegionOutlives(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::Subtype(..) - | ty::Predicate::ConstEvaluatable(..) => None, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => None, } }) .collect() @@ -313,7 +314,8 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { | ty::Predicate::ObjectSafe(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::TypeOutlives(..) - | ty::Predicate::ConstEvaluatable(..) => false, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => false, }) } @@ -724,51 +726,65 @@ fn contains_illegal_self_type_reference<'tcx>( // object type, and we cannot resolve `Self as SomeOtherTrait` // without knowing what `Self` is. - let mut supertraits: Option>> = None; - let self_ty = tcx.types.self_param; - - let mut walker = ty.walk(); - while let Some(arg) = walker.next() { - if arg == self_ty.into() { - return true; - } - - // Special-case projections (everything else is walked normally). - if let GenericArgKind::Type(ty) = arg.unpack() { - if let ty::Projection(ref data) = ty.kind { - // This is a projected type `::X`. + struct IllegalSelfTypeVisitor<'tcx> { + tcx: TyCtxt<'tcx>, + self_ty: Ty<'tcx>, + trait_def_id: DefId, + supertraits: Option>>, + } - // Compute supertraits of current trait lazily. - if supertraits.is_none() { - let trait_ref = ty::Binder::bind(ty::TraitRef::identity(tcx, trait_def_id)); - supertraits = Some(traits::supertraits(tcx, trait_ref).collect()); - } + impl<'tcx> TypeVisitor<'tcx> for IllegalSelfTypeVisitor<'tcx> { + fn visit_ty(&mut self, t: Ty<'tcx>) -> bool { + match t.kind { + ty::Param(_) => t == self.self_ty, + ty::Projection(ref data) => { + // This is a projected type `::X`. + + // Compute supertraits of current trait lazily. + if self.supertraits.is_none() { + let trait_ref = + ty::Binder::bind(ty::TraitRef::identity(self.tcx, self.trait_def_id)); + self.supertraits = Some(traits::supertraits(self.tcx, trait_ref).collect()); + } - // Determine whether the trait reference `Foo as - // SomeTrait` is in fact a supertrait of the - // current trait. In that case, this type is - // legal, because the type `X` will be specified - // in the object type. Note that we can just use - // direct equality here because all of these types - // are part of the formal parameter listing, and - // hence there should be no inference variables. - let projection_trait_ref = ty::Binder::bind(data.trait_ref(tcx)); - let is_supertrait_of_current_trait = - supertraits.as_ref().unwrap().contains(&projection_trait_ref); - - if is_supertrait_of_current_trait { - // Do not walk contained types, do not report error, do collect $200. - walker.skip_current_subtree(); + // Determine whether the trait reference `Foo as + // SomeTrait` is in fact a supertrait of the + // current trait. In that case, this type is + // legal, because the type `X` will be specified + // in the object type. Note that we can just use + // direct equality here because all of these types + // are part of the formal parameter listing, and + // hence there should be no inference variables. + let projection_trait_ref = ty::Binder::bind(data.trait_ref(self.tcx)); + let is_supertrait_of_current_trait = + self.supertraits.as_ref().unwrap().contains(&projection_trait_ref); + + if is_supertrait_of_current_trait { + false // do not walk contained types, do not report error, do collect $200 + } else { + t.super_visit_with(self) // DO walk contained types, POSSIBLY reporting an error + } } - - // DO walk contained types, POSSIBLY reporting an error. + _ => t.super_visit_with(self), // walk contained types, if any } } - // Walk contained types, if any. + fn visit_const(&mut self, _c: &ty::Const<'tcx>) -> bool { + // FIXME Look into the unevaluated constants for object safety violations. + // Do not walk substitutions of unevaluated consts, as they contain `Self`, even + // though the const expression doesn't necessary use it. Currently type variables + // inside array length expressions are forbidden, so they can't break the above + // rules. + false + } } - false + ty.visit_with(&mut IllegalSelfTypeVisitor { + tcx, + self_ty: tcx.types.self_param, + trait_def_id, + supertraits: None, + }) } pub fn provide(providers: &mut ty::query::Providers<'_>) { diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index 2b4a0409fd1e2..4a28d38eb71df 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -385,11 +385,6 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { _ => ty, } } - - fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - let constant = constant.super_fold_with(self); - constant.eval(self.selcx.tcx(), self.param_env) - } } /// The guts of `normalize`: normalize a specific projection like ` TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> { _ => ty, } } - - fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - let constant = constant.super_fold_with(self); - constant.eval(self.infcx.tcx, self.param_env) - } } diff --git a/src/librustc_trait_selection/traits/select.rs b/src/librustc_trait_selection/traits/select.rs index dfbb07424487d..f75f57c852cff 100644 --- a/src/librustc_trait_selection/traits/select.rs +++ b/src/librustc_trait_selection/traits/select.rs @@ -42,6 +42,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::lang_items; use rustc_index::bit_set::GrowableBitSet; use rustc_middle::dep_graph::{DepKind, DepNodeIndex}; +use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::fast_reject; use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef}; @@ -502,9 +503,43 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None, ) { Ok(_) => Ok(EvaluatedToOk), + Err(ErrorHandled::TooGeneric) => Ok(EvaluatedToAmbig), Err(_) => Ok(EvaluatedToErr), } } + + ty::Predicate::ConstEquate(c1, c2) => { + debug!("evaluate_predicate_recursively: equating consts c1={:?} c2={:?}", c1, c2); + + let evaluate = |c: &'tcx ty::Const<'tcx>| { + if let ty::ConstKind::Unevaluated(def_id, substs, promoted) = c.val { + match self.infcx.const_eval_resolve( + obligation.param_env, + def_id, + substs, + promoted, + Some(obligation.cause.span), + ) { + Ok(val) => Ok(ty::Const::from_value(self.tcx(), val, c.ty)), + Err(ErrorHandled::TooGeneric) => Err(EvaluatedToAmbig), + Err(_) => Err(EvaluatedToErr), + } + } else { + Ok(c) + } + }; + + match (evaluate(c1), evaluate(c2)) { + (Ok(c1), Ok(c2)) => { + match self.infcx().at(&obligation.cause, obligation.param_env).eq(c1, c2) { + Ok(_) => Ok(EvaluatedToOk), + Err(_) => Ok(EvaluatedToErr), + } + } + (Err(EvaluatedToErr), _) | (_, Err(EvaluatedToErr)) => Ok(EvaluatedToErr), + _ => Ok(EvaluatedToAmbig), + } + } } } diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 1d4afeaae42c4..a2085aed75974 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -102,6 +102,10 @@ pub fn predicate_obligations<'a, 'tcx>( wf.compute(ty); } } + ty::Predicate::ConstEquate(c1, c2) => { + wf.compute(c1.ty); + wf.compute(c2.ty); + } } wf.normalize() diff --git a/src/librustc_traits/implied_outlives_bounds.rs b/src/librustc_traits/implied_outlives_bounds.rs index 6db2e557fea69..eaaab87ab7474 100644 --- a/src/librustc_traits/implied_outlives_bounds.rs +++ b/src/librustc_traits/implied_outlives_bounds.rs @@ -100,7 +100,8 @@ fn compute_implied_outlives_bounds<'tcx>( | ty::Predicate::Projection(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::ObjectSafe(..) - | ty::Predicate::ConstEvaluatable(..) => vec![], + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => vec![], ty::Predicate::WellFormed(subty) => { wf_types.push(subty); diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index ad6c753edff00..ed30ed5313e5c 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -48,6 +48,7 @@ fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool { | ty::Predicate::ObjectSafe(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::Subtype(..) - | ty::Predicate::ConstEvaluatable(..) => true, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => true, } } diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 03e32c21a54ac..d158ccb8ebc74 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -810,7 +810,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | ty::Predicate::ObjectSafe(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::TypeOutlives(..) - | ty::Predicate::ConstEvaluatable(..) => None, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => None, }); self.elaborate_bounds(bounds, |this, poly_trait_ref, item| { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index bff1ca2433a83..d336b6940467f 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1655,6 +1655,15 @@ fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: LocalDefId, r.super_visit_with(self) } + + fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool { + if let ty::ConstKind::Unevaluated(..) = c.val { + // FIXME This check detect lifetimes within substs which violates this check even + // though the particular substitution is not used within the const. + return false; + } + c.super_visit_with(self) + } } let prohibit_opaque = match item.kind { @@ -3865,6 +3874,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty::Predicate::WellFormed(..) => None, ty::Predicate::ObjectSafe(..) => None, ty::Predicate::ConstEvaluatable(..) => None, + ty::Predicate::ConstEquate(..) => None, // N.B., this predicate is created by breaking down a // `ClosureType: FnFoo()` predicate, where // `ClosureType` represents some `Closure`. It can't diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 0fad328459890..6a2f933f3a875 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1156,7 +1156,8 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { let node = tcx.hir().get(hir_id); let parent_def_id = match node { - Node::ImplItem(_) + Node::AnonConst(_) + | Node::ImplItem(_) | Node::TraitItem(_) | Node::Variant(_) | Node::Ctor(..) @@ -1164,34 +1165,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { let parent_id = tcx.hir().get_parent_item(hir_id); Some(tcx.hir().local_def_id(parent_id).to_def_id()) } - // FIXME(#43408) enable this always when we get lazy normalization. - Node::AnonConst(_) => { - let parent_id = tcx.hir().get_parent_item(hir_id); - let parent_def_id = tcx.hir().local_def_id(parent_id); - - // HACK(eddyb) this provides the correct generics when - // `feature(const_generics)` is enabled, so that const expressions - // used with const generics, e.g. `Foo<{N+1}>`, can work at all. - if tcx.features().const_generics { - Some(parent_def_id.to_def_id()) - } else { - let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id)); - match parent_node { - // HACK(eddyb) this provides the correct generics for repeat - // expressions' count (i.e. `N` in `[x; N]`), and explicit - // `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`), - // as they shouldn't be able to cause query cycle errors. - Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. }) - | Node::Variant(Variant { disr_expr: Some(ref constant), .. }) - if constant.hir_id == hir_id => - { - Some(parent_def_id.to_def_id()) - } - _ => None, - } - } - } Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { Some(tcx.closure_base_def_id(def_id)) } diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index 8b12535b3a2cf..919bcc9943d48 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -413,6 +413,7 @@ fn trait_predicate_kind<'tcx>( | ty::Predicate::Subtype(_) | ty::Predicate::ObjectSafe(_) | ty::Predicate::ClosureKind(..) - | ty::Predicate::ConstEvaluatable(..) => None, + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => None, } } diff --git a/src/librustc_typeck/outlives/explicit.rs b/src/librustc_typeck/outlives/explicit.rs index 168f20771476c..66daf0e7f7d9d 100644 --- a/src/librustc_typeck/outlives/explicit.rs +++ b/src/librustc_typeck/outlives/explicit.rs @@ -58,7 +58,8 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { | ty::Predicate::ObjectSafe(..) | ty::Predicate::ClosureKind(..) | ty::Predicate::Subtype(..) - | ty::Predicate::ConstEvaluatable(..) => (), + | ty::Predicate::ConstEvaluatable(..) + | ty::Predicate::ConstEquate(..) => (), } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 55c32e917f25c..7d5cafd8ec048 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -493,7 +493,8 @@ impl<'a> Clean> for ty::Predicate<'a> { Predicate::WellFormed(..) | Predicate::ObjectSafe(..) | Predicate::ClosureKind(..) - | Predicate::ConstEvaluatable(..) => panic!("not user writable"), + | Predicate::ConstEvaluatable(..) + | Predicate::ConstEquate(..) => panic!("not user writable"), } } } From 4ce96a72c08256b1092cbc36d92275d043d14bda Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Thu, 2 Jan 2020 15:41:34 +1300 Subject: [PATCH 02/17] Emit `ConstEquate` obligation after checking/unifying for inference variables. This means a inference variable can be unified with an unevaluated const. --- src/librustc_infer/infer/combine.rs | 17 ++++++++++++++++- src/librustc_infer/infer/equate.rs | 22 +++++++++------------- src/librustc_infer/infer/glb.rs | 19 ++++++++----------- src/librustc_infer/infer/lub.rs | 19 ++++++++----------- src/librustc_infer/infer/nll_relate/mod.rs | 18 ++++++++++-------- src/librustc_infer/infer/sub.rs | 19 ++++++++----------- 6 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index 541d86ab9945d..24aa8750e0004 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -126,7 +126,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> where - R: TypeRelation<'tcx>, + R: ConstEquateRelation<'tcx>, { debug!("{}.consts({:?}, {:?})", relation.tag(), a, b); if a == b { @@ -164,6 +164,14 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { return self.unify_const_variable(!a_is_expected, vid, a); } + (ty::ConstKind::Unevaluated(..), _) => { + relation.const_equate_obligation(a, b); + return Ok(b); + } + (_, ty::ConstKind::Unevaluated(..)) => { + relation.const_equate_obligation(a, b); + return Ok(a); + } _ => {} } @@ -654,6 +662,13 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { } } +pub trait ConstEquateRelation<'tcx>: TypeRelation<'tcx> { + /// Register am obligation that both constants must be equal to each other. + /// + /// If they aren't equal then the relation doesn't hold. + fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>); +} + pub trait RelateResultCompare<'tcx, T> { fn compare(&self, t: T, f: F) -> RelateResult<'tcx, T> where diff --git a/src/librustc_infer/infer/equate.rs b/src/librustc_infer/infer/equate.rs index 160f746bdf7ef..ca5e60efbf8b9 100644 --- a/src/librustc_infer/infer/equate.rs +++ b/src/librustc_infer/infer/equate.rs @@ -1,10 +1,10 @@ -use super::combine::{CombineFields, RelationDir}; +use super::combine::{CombineFields, RelationDir, ConstEquateRelation}; use super::Subtype; use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation}; use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::TyVar; -use rustc_middle::ty::{self, ConstKind, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_hir::def_id::DefId; @@ -119,17 +119,7 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - match (a.val, b.val) { - (ConstKind::Unevaluated(..), _) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(b) - } - (_, ConstKind::Unevaluated(..)) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(a) - } - _ => self.fields.infcx.super_combine_consts(self, a, b), - } + self.fields.infcx.super_combine_consts(self, a, b) } fn binders( @@ -150,3 +140,9 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> { } } } + +impl<'tcx> ConstEquateRelation<'tcx> for Equate<'_, '_, 'tcx> { + fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + } +} diff --git a/src/librustc_infer/infer/glb.rs b/src/librustc_infer/infer/glb.rs index 583e80efc7c55..ec219a95b9441 100644 --- a/src/librustc_infer/infer/glb.rs +++ b/src/librustc_infer/infer/glb.rs @@ -3,6 +3,7 @@ use super::lattice::{self, LatticeDir}; use super::InferCtxt; use super::Subtype; +use crate::infer::combine::ConstEquateRelation; use crate::traits::ObligationCause; use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation}; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -79,17 +80,7 @@ impl TypeRelation<'tcx> for Glb<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - match (a.val, b.val) { - (ty::ConstKind::Unevaluated(..), _) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(b) - } - (_, ty::ConstKind::Unevaluated(..)) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(a) - } - _ => self.fields.infcx.super_combine_consts(self, a, b), - } + self.fields.infcx.super_combine_consts(self, a, b) } fn binders( @@ -126,3 +117,9 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx, Ok(()) } } + +impl<'tcx> ConstEquateRelation<'tcx> for Glb<'_, '_, 'tcx> { + fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + } +} diff --git a/src/librustc_infer/infer/lub.rs b/src/librustc_infer/infer/lub.rs index e613bd5dc7a89..a0453db2cb499 100644 --- a/src/librustc_infer/infer/lub.rs +++ b/src/librustc_infer/infer/lub.rs @@ -3,6 +3,7 @@ use super::lattice::{self, LatticeDir}; use super::InferCtxt; use super::Subtype; +use crate::infer::combine::ConstEquateRelation; use crate::traits::ObligationCause; use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation}; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -79,17 +80,7 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - match (a.val, b.val) { - (ty::ConstKind::Unevaluated(..), _) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(b) - } - (_, ty::ConstKind::Unevaluated(..)) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(a) - } - _ => self.fields.infcx.super_combine_consts(self, a, b), - } + self.fields.infcx.super_combine_consts(self, a, b) } fn binders( @@ -110,6 +101,12 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> { } } +impl<'tcx> ConstEquateRelation<'tcx> for Lub<'_, '_, 'tcx> { + fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + } +} + impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> { fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'tcx> { self.fields.infcx diff --git a/src/librustc_infer/infer/nll_relate/mod.rs b/src/librustc_infer/infer/nll_relate/mod.rs index 2787d7fef91a7..500cab29e3acc 100644 --- a/src/librustc_infer/infer/nll_relate/mod.rs +++ b/src/librustc_infer/infer/nll_relate/mod.rs @@ -21,6 +21,7 @@ //! thing we relate in chalk are basically domain goals and their //! constituents) +use crate::infer::combine::ConstEquateRelation; use crate::infer::InferCtxt; use crate::infer::{ConstVarValue, ConstVariableValue}; use rustc_data_structures::fx::FxHashMap; @@ -595,14 +596,6 @@ where } match (a.val, b.val) { - (ty::ConstKind::Unevaluated(..), _) => { - self.delegate.const_equate(a, b); - Ok(b) - } - (_, ty::ConstKind::Unevaluated(..)) => { - self.delegate.const_equate(a, b); - Ok(a) - } (_, ty::ConstKind::Infer(InferConst::Var(_))) if D::forbid_inference_vars() => { // Forbid inference variables in the RHS. bug!("unexpected inference var {:?}", b) @@ -725,6 +718,15 @@ where } } +impl<'tcx, D> ConstEquateRelation<'tcx> for TypeRelating<'_, 'tcx, D> +where + D: TypeRelatingDelegate<'tcx>, +{ + fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) { + self.delegate.const_equate(a, b); + } +} + /// When we encounter a binder like `for<..> fn(..)`, we actually have /// to walk the `fn` value to find all the values bound by the `for` /// (these are not explicitly present in the ty representation right diff --git a/src/librustc_infer/infer/sub.rs b/src/librustc_infer/infer/sub.rs index a432fd7d1a130..1c6ae9cd7e67b 100644 --- a/src/librustc_infer/infer/sub.rs +++ b/src/librustc_infer/infer/sub.rs @@ -1,6 +1,7 @@ use super::combine::{CombineFields, RelationDir}; use super::SubregionOrigin; +use crate::infer::combine::ConstEquateRelation; use crate::traits::Obligation; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation}; @@ -155,17 +156,7 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> { a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - match (a.val, b.val) { - (ty::ConstKind::Unevaluated(..), _) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(b) - } - (_, ty::ConstKind::Unevaluated(..)) => { - self.fields.add_const_equate_obligation(self.a_is_expected, a, b); - Ok(a) - } - _ => self.fields.infcx.super_combine_consts(self, a, b), - } + self.fields.infcx.super_combine_consts(self, a, b) } fn binders( @@ -179,3 +170,9 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> { self.fields.higher_ranked_sub(a, b, self.a_is_expected) } } + +impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> { + fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) { + self.fields.add_const_equate_obligation(self.a_is_expected, a, b); + } +} From 840d2f5f1360ba9b61cb6de88c262204c1d1b41e Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Sat, 4 Jan 2020 13:42:02 +1300 Subject: [PATCH 03/17] Add lazy normalization tests --- .../ui/const-generics/issues/issue-61935.rs | 24 ++++++++ .../const-generics/issues/issue-61935.stderr | 8 +++ .../ui/const-generics/issues/issue-67185-1.rs | 32 ++++++++++ .../issues/issue-67185-1.stderr | 8 +++ .../ui/const-generics/issues/issue-67185-2.rs | 34 +++++++++++ .../issues/issue-67185-2.stderr | 61 +++++++++++++++++++ .../ui/const-generics/trait-const-args.rs | 29 +++++++++ .../ui/const-generics/trait-const-args.stderr | 8 +++ 8 files changed, 204 insertions(+) create mode 100644 src/test/ui/const-generics/issues/issue-61935.rs create mode 100644 src/test/ui/const-generics/issues/issue-61935.stderr create mode 100644 src/test/ui/const-generics/issues/issue-67185-1.rs create mode 100644 src/test/ui/const-generics/issues/issue-67185-1.stderr create mode 100644 src/test/ui/const-generics/issues/issue-67185-2.rs create mode 100644 src/test/ui/const-generics/issues/issue-67185-2.stderr create mode 100644 src/test/ui/const-generics/trait-const-args.rs create mode 100644 src/test/ui/const-generics/trait-const-args.stderr diff --git a/src/test/ui/const-generics/issues/issue-61935.rs b/src/test/ui/const-generics/issues/issue-61935.rs new file mode 100644 index 0000000000000..35fb435b812a4 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-61935.rs @@ -0,0 +1,24 @@ +// check-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +trait Foo {} + +impl Foo for [(); N] + where + Self:FooImpl<{N==0}> +{} + +trait FooImpl{} + +impl FooImpl for [(); 0] {} + +impl FooImpl for [();N] {} + +fn foo(_: impl Foo) {} + +fn main() { + foo([]); + foo([()]); +} diff --git a/src/test/ui/const-generics/issues/issue-61935.stderr b/src/test/ui/const-generics/issues/issue-61935.stderr new file mode 100644 index 0000000000000..ab7443f17664d --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-61935.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/issue-61935.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + diff --git a/src/test/ui/const-generics/issues/issue-67185-1.rs b/src/test/ui/const-generics/issues/issue-67185-1.rs new file mode 100644 index 0000000000000..89e0b7f62da5d --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-67185-1.rs @@ -0,0 +1,32 @@ +// check-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +trait Baz { + type Quaks; +} +impl Baz for u8 { + type Quaks = [u16; 3]; +} + +trait Bar {} +impl Bar for [u16; 3] {} +impl Bar for [[u16; 3]; 2] {} + +trait Foo + where + [::Quaks; 2]: Bar, + ::Quaks: Bar, +{ +} + +struct FooImpl; + +impl Foo for FooImpl {} + +fn f(_: impl Foo) {} + +fn main() { + f(FooImpl) +} diff --git a/src/test/ui/const-generics/issues/issue-67185-1.stderr b/src/test/ui/const-generics/issues/issue-67185-1.stderr new file mode 100644 index 0000000000000..01c09763314a9 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-67185-1.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/issue-67185-1.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + diff --git a/src/test/ui/const-generics/issues/issue-67185-2.rs b/src/test/ui/const-generics/issues/issue-67185-2.rs new file mode 100644 index 0000000000000..af797721324e3 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-67185-2.rs @@ -0,0 +1,34 @@ +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +trait Baz { + type Quaks; +} +impl Baz for u8 { + type Quaks = [u16; 3]; +} + +trait Bar {} +impl Bar for [u16; 4] {} +impl Bar for [[u16; 3]; 3] {} + +trait Foo //~ ERROR mismatched types + where + [::Quaks; 2]: Bar, + ::Quaks: Bar, +{ +} + +struct FooImpl; + +impl Foo for FooImpl {} +//~^ ERROR mismatched types +//~^^ ERROR mismatched types + +fn f(_: impl Foo) {} +//~^ ERROR mismatched types +//~^^ ERROR mismatched types + +fn main() { + f(FooImpl) +} diff --git a/src/test/ui/const-generics/issues/issue-67185-2.stderr b/src/test/ui/const-generics/issues/issue-67185-2.stderr new file mode 100644 index 0000000000000..3a46d8fece88f --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-67185-2.stderr @@ -0,0 +1,61 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/issue-67185-2.rs:1:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + +error[E0308]: mismatched types + --> $DIR/issue-67185-2.rs:15:1 + | +LL | / trait Foo +LL | | where +LL | | [::Quaks; 2]: Bar, +LL | | ::Quaks: Bar, +LL | | { +LL | | } + | |_^ expected `3usize`, found `4usize` + | + = note: expected type `3usize` + found type `4usize` + +error[E0308]: mismatched types + --> $DIR/issue-67185-2.rs:24:6 + | +LL | impl Foo for FooImpl {} + | ^^^ expected `3usize`, found `4usize` + | + = note: expected type `3usize` + found type `4usize` + +error[E0308]: mismatched types + --> $DIR/issue-67185-2.rs:24:6 + | +LL | impl Foo for FooImpl {} + | ^^^ expected `2usize`, found `3usize` + | + = note: expected type `2usize` + found type `3usize` + +error[E0308]: mismatched types + --> $DIR/issue-67185-2.rs:28:1 + | +LL | fn f(_: impl Foo) {} + | ^^^^^^^^^^^^^^^^^^^^ expected `2usize`, found `3usize` + | + = note: expected type `2usize` + found type `3usize` + +error[E0308]: mismatched types + --> $DIR/issue-67185-2.rs:28:1 + | +LL | fn f(_: impl Foo) {} + | ^^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `4usize` + | + = note: expected type `3usize` + found type `4usize` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/const-generics/trait-const-args.rs b/src/test/ui/const-generics/trait-const-args.rs new file mode 100644 index 0000000000000..f7fef815e0769 --- /dev/null +++ b/src/test/ui/const-generics/trait-const-args.rs @@ -0,0 +1,29 @@ +// check-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +struct Const; +trait Foo {} + +impl Foo<{N}> for Const<{N}> {} + +fn foo_impl(_: impl Foo<3>) {} + +fn foo_explicit>(_: T) {} + +fn foo_where(_: T) where T: Foo<3> {} + +fn main() { + // FIXME this causes a stack overflow in rustc + // foo_impl(Const); + foo_impl(Const::<3>); + + // FIXME this causes a stack overflow in rustc + // foo_explicit(Const); + foo_explicit(Const::<3>); + + // FIXME this causes a stack overflow in rustc + // foo_where(Const); + foo_where(Const::<3>); +} \ No newline at end of file diff --git a/src/test/ui/const-generics/trait-const-args.stderr b/src/test/ui/const-generics/trait-const-args.stderr new file mode 100644 index 0000000000000..98e9046e5d5a1 --- /dev/null +++ b/src/test/ui/const-generics/trait-const-args.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/trait-const-args.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + From 49b9d9441ef8403fc3d53bedd4d3ab270bed2bb3 Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Sat, 4 Jan 2020 18:54:19 +1300 Subject: [PATCH 04/17] Put lazy normalization behind a -Z flag --- src/librustc_infer/infer/combine.rs | 6 ++-- src/librustc_infer/infer/nll_relate/mod.rs | 2 +- src/librustc_middle/ty/relate.rs | 40 ++++++++++++---------- src/librustc_session/options.rs | 2 ++ src/librustc_typeck/collect.rs | 12 +++++-- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index 24aa8750e0004..e18673e9bc887 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -164,11 +164,11 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { return self.unify_const_variable(!a_is_expected, vid, a); } - (ty::ConstKind::Unevaluated(..), _) => { + (ty::ConstKind::Unevaluated(..), _) if self.tcx.sess.opts.debugging_opts.lazy_normalization => { relation.const_equate_obligation(a, b); return Ok(b); } - (_, ty::ConstKind::Unevaluated(..)) => { + (_, ty::ConstKind::Unevaluated(..)) if self.tcx.sess.opts.debugging_opts.lazy_normalization => { relation.const_equate_obligation(a, b); return Ok(a); } @@ -656,7 +656,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { } } } - ty::ConstKind::Unevaluated(..) => Ok(c), + ty::ConstKind::Unevaluated(..) if self.tcx().sess.opts.debugging_opts.lazy_normalization => Ok(c), _ => relate::super_relate_consts(self, c, c), } } diff --git a/src/librustc_infer/infer/nll_relate/mod.rs b/src/librustc_infer/infer/nll_relate/mod.rs index 500cab29e3acc..e380fb91ce296 100644 --- a/src/librustc_infer/infer/nll_relate/mod.rs +++ b/src/librustc_infer/infer/nll_relate/mod.rs @@ -986,7 +986,7 @@ where } } } - ty::ConstKind::Unevaluated(..) => Ok(a), + ty::ConstKind::Unevaluated(..) if self.tcx().sess.opts.debugging_opts.lazy_normalization=> Ok(a), _ => relate::super_relate_consts(self, a, a), } } diff --git a/src/librustc_middle/ty/relate.rs b/src/librustc_middle/ty/relate.rs index 225023618f73f..065194aeee839 100644 --- a/src/librustc_middle/ty/relate.rs +++ b/src/librustc_middle/ty/relate.rs @@ -431,18 +431,20 @@ pub fn super_relate_tys>( let t = relation.relate(&a_t, &b_t)?; match relation.relate(&sz_a, &sz_b) { Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))), + // FIXME(lazy-normalization) Implement improved diagnostics for mismatched array + // length? + Err(err) if relation.tcx().sess.opts.debugging_opts.lazy_normalization => Err(err), Err(err) => { - // // Check whether the lengths are both concrete/known values, - // // but are unequal, for better diagnostics. - // let sz_a = sz_a.try_eval_usize(tcx, relation.param_env()); - // let sz_b = sz_b.try_eval_usize(tcx, relation.param_env()); - // match (sz_a, sz_b) { - // (Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize( - // expected_found(relation, &sz_a_val, &sz_b_val), - // )), - // _ => Err(err), - // } - Err(err) + // Check whether the lengths are both concrete/known values, + // but are unequal, for better diagnostics. + let sz_a = sz_a.try_eval_usize(tcx, relation.param_env()); + let sz_b = sz_b.try_eval_usize(tcx, relation.param_env()); + match (sz_a, sz_b) { + (Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize( + expected_found(relation, &sz_a_val, &sz_b_val), + )), + _ => Err(err), + } } } } @@ -611,14 +613,14 @@ pub fn super_relate_consts>( } // FIXME(const_generics): this is wrong, as it is a projection - // ( - // ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted), - // ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted), - // ) if a_def_id == b_def_id && a_promoted == b_promoted => { - // let substs = - // relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?; - // Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted)) - // } + ( + ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted), + ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted), + ) if a_def_id == b_def_id && a_promoted == b_promoted => { + let substs = + relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?; + Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted)) + } _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))), }; new_const_val.map(|val| tcx.mk_const(ty::Const { val, ty: a.ty })) diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs index 984d47956ca59..a263658e76045 100644 --- a/src/librustc_session/options.rs +++ b/src/librustc_session/options.rs @@ -844,6 +844,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "insert function instrument code for mcount-based tracing (default: no)"), keep_hygiene_data: bool = (false, parse_bool, [UNTRACKED], "keep hygiene data after analysis (default: no)"), + lazy_normalization: bool = (false, parse_bool, [UNTRACKED], + "lazily evaluate constants (experimental)"), link_native_libraries: bool = (true, parse_bool, [UNTRACKED], "link native libraries in the linker invocation (default: yes)"), link_only: bool = (false, parse_bool, [TRACKED], diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 6a2f933f3a875..721e152072428 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1156,8 +1156,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { let node = tcx.hir().get(hir_id); let parent_def_id = match node { - Node::AnonConst(_) - | Node::ImplItem(_) + Node::ImplItem(_) | Node::TraitItem(_) | Node::Variant(_) | Node::Ctor(..) @@ -1166,6 +1165,15 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { Some(tcx.hir().local_def_id(parent_id).to_def_id()) } + Node::AnonConst(_) => { + if tcx.sess.opts.debugging_opts.lazy_normalization { + let parent_id = tcx.hir().get_parent_item(hir_id); + Some(tcx.hir().local_def_id(parent_id)) + } else { + None + } + } + Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { Some(tcx.closure_base_def_id(def_id)) } From 3ad1a05e537c8e4594f3e694e9aa86de1c3c5160 Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Sun, 5 Jan 2020 14:09:25 +1300 Subject: [PATCH 05/17] Fix tests and lazy normalization gating. Also adds `-Z lazy-normalization` to some tests that require lazy normalization to pass. --- src/librustc_infer/infer/combine.rs | 14 +++++++++++--- src/librustc_infer/infer/nll_relate/mod.rs | 6 +++++- src/librustc_trait_selection/traits/project.rs | 8 ++++++++ .../traits/query/normalize.rs | 4 ++++ src/librustc_typeck/collect.rs | 7 ++++++- src/test/ui/const-generics/issues/issue-61935.rs | 1 + .../ui/const-generics/issues/issue-61935.stderr | 2 +- src/test/ui/const-generics/issues/issue-67185-1.rs | 1 + .../ui/const-generics/issues/issue-67185-1.stderr | 2 +- src/test/ui/const-generics/issues/issue-67185-2.rs | 2 ++ .../ui/const-generics/issues/issue-67185-2.stderr | 12 ++++++------ src/test/ui/const-generics/trait-const-args.rs | 3 ++- src/test/ui/const-generics/trait-const-args.stderr | 2 +- 13 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index e18673e9bc887..6f27d377f49f1 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -164,11 +164,15 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { return self.unify_const_variable(!a_is_expected, vid, a); } - (ty::ConstKind::Unevaluated(..), _) if self.tcx.sess.opts.debugging_opts.lazy_normalization => { + (ty::ConstKind::Unevaluated(..), _) + if self.tcx.sess.opts.debugging_opts.lazy_normalization => + { relation.const_equate_obligation(a, b); return Ok(b); } - (_, ty::ConstKind::Unevaluated(..)) if self.tcx.sess.opts.debugging_opts.lazy_normalization => { + (_, ty::ConstKind::Unevaluated(..)) + if self.tcx.sess.opts.debugging_opts.lazy_normalization => + { relation.const_equate_obligation(a, b); return Ok(a); } @@ -656,7 +660,11 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { } } } - ty::ConstKind::Unevaluated(..) if self.tcx().sess.opts.debugging_opts.lazy_normalization => Ok(c), + ty::ConstKind::Unevaluated(..) + if self.tcx().sess.opts.debugging_opts.lazy_normalization => + { + Ok(c) + } _ => relate::super_relate_consts(self, c, c), } } diff --git a/src/librustc_infer/infer/nll_relate/mod.rs b/src/librustc_infer/infer/nll_relate/mod.rs index e380fb91ce296..e5ef9ec72ff0c 100644 --- a/src/librustc_infer/infer/nll_relate/mod.rs +++ b/src/librustc_infer/infer/nll_relate/mod.rs @@ -986,7 +986,11 @@ where } } } - ty::ConstKind::Unevaluated(..) if self.tcx().sess.opts.debugging_opts.lazy_normalization=> Ok(a), + ty::ConstKind::Unevaluated(..) + if self.tcx().sess.opts.debugging_opts.lazy_normalization => + { + Ok(a) + } _ => relate::super_relate_consts(self, a, a), } } diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index 4a28d38eb71df..b55bf5ba530de 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -385,6 +385,14 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { _ => ty, } } + + fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + if self.selcx.tcx().sess.opts.debugging_opts.lazy_normalization { + constant + } else { + constant.eval(self.selcx.tcx(), self.param_env) + } + } } /// The guts of `normalize`: normalize a specific projection like ` TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> { _ => ty, } } + + fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + constant.eval(self.infcx.tcx, self.param_env) + } } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 721e152072428..3f81b4c0409c9 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1166,7 +1166,12 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { } Node::AnonConst(_) => { - if tcx.sess.opts.debugging_opts.lazy_normalization { + // HACK(skinny121) Provide correct generics if `feature(const_generics)` is enabled, in + // addition to when the '-Z lazy-normalization' flag is used, so that trait + // implementations that have const generic parameters within the standard library still + // work. The feature check won't be necessary when lazy normalization is enabled by + // default. + if tcx.sess.opts.debugging_opts.lazy_normalization || tcx.features().const_generics { let parent_id = tcx.hir().get_parent_item(hir_id); Some(tcx.hir().local_def_id(parent_id)) } else { diff --git a/src/test/ui/const-generics/issues/issue-61935.rs b/src/test/ui/const-generics/issues/issue-61935.rs index 35fb435b812a4..5277d8ec27ec4 100644 --- a/src/test/ui/const-generics/issues/issue-61935.rs +++ b/src/test/ui/const-generics/issues/issue-61935.rs @@ -1,4 +1,5 @@ // check-pass +// compile-flags: -Z lazy-normalization #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash diff --git a/src/test/ui/const-generics/issues/issue-61935.stderr b/src/test/ui/const-generics/issues/issue-61935.stderr index ab7443f17664d..cc27dd6f3ae19 100644 --- a/src/test/ui/const-generics/issues/issue-61935.stderr +++ b/src/test/ui/const-generics/issues/issue-61935.stderr @@ -1,5 +1,5 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-61935.rs:3:12 + --> $DIR/issue-61935.rs:4:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/const-generics/issues/issue-67185-1.rs b/src/test/ui/const-generics/issues/issue-67185-1.rs index 89e0b7f62da5d..a38049ed26ffb 100644 --- a/src/test/ui/const-generics/issues/issue-67185-1.rs +++ b/src/test/ui/const-generics/issues/issue-67185-1.rs @@ -1,4 +1,5 @@ // check-pass +// compile-flags: -Z lazy-normalization #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash diff --git a/src/test/ui/const-generics/issues/issue-67185-1.stderr b/src/test/ui/const-generics/issues/issue-67185-1.stderr index 01c09763314a9..211cd1628d2f0 100644 --- a/src/test/ui/const-generics/issues/issue-67185-1.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-1.stderr @@ -1,5 +1,5 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-67185-1.rs:3:12 + --> $DIR/issue-67185-1.rs:4:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/const-generics/issues/issue-67185-2.rs b/src/test/ui/const-generics/issues/issue-67185-2.rs index af797721324e3..f0f06339fe0de 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.rs +++ b/src/test/ui/const-generics/issues/issue-67185-2.rs @@ -1,3 +1,5 @@ +// compile-flags: -Z lazy-normalization + #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash diff --git a/src/test/ui/const-generics/issues/issue-67185-2.stderr b/src/test/ui/const-generics/issues/issue-67185-2.stderr index 3a46d8fece88f..215093364c6d6 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-2.stderr @@ -1,5 +1,5 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-67185-2.rs:1:12 + --> $DIR/issue-67185-2.rs:3:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ @@ -7,7 +7,7 @@ LL | #![feature(const_generics)] = note: `#[warn(incomplete_features)]` on by default error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:15:1 + --> $DIR/issue-67185-2.rs:17:1 | LL | / trait Foo LL | | where @@ -21,7 +21,7 @@ LL | | } found type `4usize` error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:24:6 + --> $DIR/issue-67185-2.rs:26:6 | LL | impl Foo for FooImpl {} | ^^^ expected `3usize`, found `4usize` @@ -30,7 +30,7 @@ LL | impl Foo for FooImpl {} found type `4usize` error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:24:6 + --> $DIR/issue-67185-2.rs:26:6 | LL | impl Foo for FooImpl {} | ^^^ expected `2usize`, found `3usize` @@ -39,7 +39,7 @@ LL | impl Foo for FooImpl {} found type `3usize` error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:28:1 + --> $DIR/issue-67185-2.rs:30:1 | LL | fn f(_: impl Foo) {} | ^^^^^^^^^^^^^^^^^^^^ expected `2usize`, found `3usize` @@ -48,7 +48,7 @@ LL | fn f(_: impl Foo) {} found type `3usize` error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:28:1 + --> $DIR/issue-67185-2.rs:30:1 | LL | fn f(_: impl Foo) {} | ^^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `4usize` diff --git a/src/test/ui/const-generics/trait-const-args.rs b/src/test/ui/const-generics/trait-const-args.rs index f7fef815e0769..7a5901069cdef 100644 --- a/src/test/ui/const-generics/trait-const-args.rs +++ b/src/test/ui/const-generics/trait-const-args.rs @@ -1,4 +1,5 @@ // check-pass +// compile-flags: -Z lazy-normalization #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash @@ -26,4 +27,4 @@ fn main() { // FIXME this causes a stack overflow in rustc // foo_where(Const); foo_where(Const::<3>); -} \ No newline at end of file +} diff --git a/src/test/ui/const-generics/trait-const-args.stderr b/src/test/ui/const-generics/trait-const-args.stderr index 98e9046e5d5a1..1e7c84fdbac99 100644 --- a/src/test/ui/const-generics/trait-const-args.stderr +++ b/src/test/ui/const-generics/trait-const-args.stderr @@ -1,5 +1,5 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/trait-const-args.rs:3:12 + --> $DIR/trait-const-args.rs:4:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ From eb5112fc455c39f5f1a9e084754a95b3aba3b4bd Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Sat, 29 Feb 2020 15:27:11 +1300 Subject: [PATCH 06/17] Added `lazy_normalization_consts` feature, and removed the -Z flag. --- src/librustc_feature/active.rs | 4 + src/librustc_infer/infer/combine.rs | 10 +- src/librustc_infer/infer/equate.rs | 2 +- src/librustc_infer/infer/nll_relate/mod.rs | 4 +- src/librustc_middle/ty/relate.rs | 4 +- src/librustc_session/options.rs | 2 - src/librustc_span/symbol.rs | 1 + .../traits/project.rs | 2 +- src/librustc_typeck/collect.rs | 7 +- .../array-size-in-generic-struct-param.rs | 2 + .../array-size-in-generic-struct-param.stderr | 10 +- .../ui/const-generics/issues/issue-61336-1.rs | 2 + .../issues/issue-61336-1.stderr | 8 ++ .../ui/const-generics/issues/issue-61336-2.rs | 2 + .../issues/issue-61336-2.stderr | 6 + .../ui/const-generics/issues/issue-61336.rs | 2 + .../const-generics/issues/issue-61336.stderr | 6 + .../ui/const-generics/issues/issue-61747.rs | 2 + .../const-generics/issues/issue-61747.stderr | 8 ++ .../ui/const-generics/issues/issue-61935.rs | 3 +- .../const-generics/issues/issue-61935.stderr | 8 +- .../ui/const-generics/issues/issue-66205.rs | 1 + .../ui/const-generics/issues/issue-67185-1.rs | 3 +- .../issues/issue-67185-1.stderr | 8 +- .../ui/const-generics/issues/issue-67185-2.rs | 15 +-- .../issues/issue-67185-2.stderr | 108 +++++++++++++----- .../ui/const-generics/trait-const-args.rs | 3 +- .../ui/const-generics/trait-const-args.stderr | 8 +- 28 files changed, 176 insertions(+), 65 deletions(-) diff --git a/src/librustc_feature/active.rs b/src/librustc_feature/active.rs index a1dd7a5ca5225..f075988099b9e 100644 --- a/src/librustc_feature/active.rs +++ b/src/librustc_feature/active.rs @@ -558,6 +558,9 @@ declare_features! ( /// Allow negative trait implementations. (active, negative_impls, "1.44.0", Some(68318), None), + + /// Lazily evaluate constants. Which allows constants to depend on type parameters. + (active, lazy_normalization_consts, "1.44.0", Some(60471), None), /// Allows the use of `#[target_feature]` on safe functions. (active, target_feature_11, "1.45.0", Some(69098), None), @@ -581,4 +584,5 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[ sym::raw_dylib, sym::const_trait_impl, sym::const_trait_bound_opt_out, + sym::lazy_normalization_consts, ]; diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index 6f27d377f49f1..fa119029ebb62 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -165,13 +165,13 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { return self.unify_const_variable(!a_is_expected, vid, a); } (ty::ConstKind::Unevaluated(..), _) - if self.tcx.sess.opts.debugging_opts.lazy_normalization => + if self.tcx.features().lazy_normalization_consts => { relation.const_equate_obligation(a, b); return Ok(b); } (_, ty::ConstKind::Unevaluated(..)) - if self.tcx.sess.opts.debugging_opts.lazy_normalization => + if self.tcx.features().lazy_normalization_consts => { relation.const_equate_obligation(a, b); return Ok(a); @@ -660,9 +660,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { } } } - ty::ConstKind::Unevaluated(..) - if self.tcx().sess.opts.debugging_opts.lazy_normalization => - { + ty::ConstKind::Unevaluated(..) if self.tcx().features().lazy_normalization_consts => { Ok(c) } _ => relate::super_relate_consts(self, c, c), @@ -671,7 +669,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { } pub trait ConstEquateRelation<'tcx>: TypeRelation<'tcx> { - /// Register am obligation that both constants must be equal to each other. + /// Register an obligation that both constants must be equal to each other. /// /// If they aren't equal then the relation doesn't hold. fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>); diff --git a/src/librustc_infer/infer/equate.rs b/src/librustc_infer/infer/equate.rs index ca5e60efbf8b9..5fcce37768a8c 100644 --- a/src/librustc_infer/infer/equate.rs +++ b/src/librustc_infer/infer/equate.rs @@ -1,4 +1,4 @@ -use super::combine::{CombineFields, RelationDir, ConstEquateRelation}; +use super::combine::{CombineFields, ConstEquateRelation, RelationDir}; use super::Subtype; use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation}; diff --git a/src/librustc_infer/infer/nll_relate/mod.rs b/src/librustc_infer/infer/nll_relate/mod.rs index e5ef9ec72ff0c..f1a4b2adcb741 100644 --- a/src/librustc_infer/infer/nll_relate/mod.rs +++ b/src/librustc_infer/infer/nll_relate/mod.rs @@ -986,9 +986,7 @@ where } } } - ty::ConstKind::Unevaluated(..) - if self.tcx().sess.opts.debugging_opts.lazy_normalization => - { + ty::ConstKind::Unevaluated(..) if self.tcx().features().lazy_normalization_consts => { Ok(a) } _ => relate::super_relate_consts(self, a, a), diff --git a/src/librustc_middle/ty/relate.rs b/src/librustc_middle/ty/relate.rs index 065194aeee839..c9bf362a49e88 100644 --- a/src/librustc_middle/ty/relate.rs +++ b/src/librustc_middle/ty/relate.rs @@ -431,9 +431,9 @@ pub fn super_relate_tys>( let t = relation.relate(&a_t, &b_t)?; match relation.relate(&sz_a, &sz_b) { Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))), - // FIXME(lazy-normalization) Implement improved diagnostics for mismatched array + // FIXME(lazy_normalization_consts) Implement improved diagnostics for mismatched array // length? - Err(err) if relation.tcx().sess.opts.debugging_opts.lazy_normalization => Err(err), + Err(err) if relation.tcx().features().lazy_normalization_consts => Err(err), Err(err) => { // Check whether the lengths are both concrete/known values, // but are unequal, for better diagnostics. diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs index a263658e76045..daa992ed44202 100644 --- a/src/librustc_session/options.rs +++ b/src/librustc_session/options.rs @@ -843,8 +843,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, instrument_mcount: bool = (false, parse_bool, [TRACKED], "insert function instrument code for mcount-based tracing (default: no)"), keep_hygiene_data: bool = (false, parse_bool, [UNTRACKED], - "keep hygiene data after analysis (default: no)"), - lazy_normalization: bool = (false, parse_bool, [UNTRACKED], "lazily evaluate constants (experimental)"), link_native_libraries: bool = (true, parse_bool, [UNTRACKED], "link native libraries in the linker invocation (default: yes)"), diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs index a61647bfd655f..e4318c65050a1 100644 --- a/src/librustc_span/symbol.rs +++ b/src/librustc_span/symbol.rs @@ -411,6 +411,7 @@ symbols! { label_break_value, lang, lang_items, + lazy_normalization_consts, let_chains, lhs, lib, diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index b55bf5ba530de..8bf45d8fc8e82 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -387,7 +387,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { } fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if self.selcx.tcx().sess.opts.debugging_opts.lazy_normalization { + if self.selcx.tcx().features().lazy_normalization_consts { constant } else { constant.eval(self.selcx.tcx(), self.param_env) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 3f81b4c0409c9..efd185d5f7f0e 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1166,12 +1166,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { } Node::AnonConst(_) => { - // HACK(skinny121) Provide correct generics if `feature(const_generics)` is enabled, in - // addition to when the '-Z lazy-normalization' flag is used, so that trait - // implementations that have const generic parameters within the standard library still - // work. The feature check won't be necessary when lazy normalization is enabled by - // default. - if tcx.sess.opts.debugging_opts.lazy_normalization || tcx.features().const_generics { + if tcx.features().lazy_normalization_consts { let parent_id = tcx.hir().get_parent_item(hir_id); Some(tcx.hir().local_def_id(parent_id)) } else { diff --git a/src/test/ui/const-generics/array-size-in-generic-struct-param.rs b/src/test/ui/const-generics/array-size-in-generic-struct-param.rs index d996bf56fcc10..d05fa4aaecc9e 100644 --- a/src/test/ui/const-generics/array-size-in-generic-struct-param.rs +++ b/src/test/ui/const-generics/array-size-in-generic-struct-param.rs @@ -1,5 +1,7 @@ #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash #[allow(dead_code)] struct ArithArrayLen([u32; 0 + N]); diff --git a/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr b/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr index 05f30a1cc5ed6..cd4526d834981 100644 --- a/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr +++ b/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr @@ -6,8 +6,14 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/array-size-in-generic-struct-param.rs:3:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + error: constant expression depends on a generic parameter - --> $DIR/array-size-in-generic-struct-param.rs:5:38 + --> $DIR/array-size-in-generic-struct-param.rs:7:38 | LL | struct ArithArrayLen([u32; 0 + N]); | ^^^^^^^^^^^^ @@ -15,7 +21,7 @@ LL | struct ArithArrayLen([u32; 0 + N]); = note: this may fail depending on what value the parameter takes error: constant expression depends on a generic parameter - --> $DIR/array-size-in-generic-struct-param.rs:14:5 + --> $DIR/array-size-in-generic-struct-param.rs:16:5 | LL | arr: [u8; CFG.arr_size], | ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/const-generics/issues/issue-61336-1.rs b/src/test/ui/const-generics/issues/issue-61336-1.rs index 165d3e1c2e601..1f3d0c2a746ee 100644 --- a/src/test/ui/const-generics/issues/issue-61336-1.rs +++ b/src/test/ui/const-generics/issues/issue-61336-1.rs @@ -1,5 +1,7 @@ #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash // build-pass diff --git a/src/test/ui/const-generics/issues/issue-61336-1.stderr b/src/test/ui/const-generics/issues/issue-61336-1.stderr index 34920d8907fc1..aa802b75683b8 100644 --- a/src/test/ui/const-generics/issues/issue-61336-1.stderr +++ b/src/test/ui/const-generics/issues/issue-61336-1.stderr @@ -6,5 +6,13 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default +<<<<<<< HEAD warning: 1 warning emitted +======= +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-61336-1.rs:3:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +>>>>>>> Added `lazy_normalization_consts` feature, and removed the -Z flag. diff --git a/src/test/ui/const-generics/issues/issue-61336-2.rs b/src/test/ui/const-generics/issues/issue-61336-2.rs index c5bf6b6ce94a8..0f4d92a9a8a94 100644 --- a/src/test/ui/const-generics/issues/issue-61336-2.rs +++ b/src/test/ui/const-generics/issues/issue-61336-2.rs @@ -1,5 +1,7 @@ #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash fn f(x: T) -> [T; N] { [x; { N }] diff --git a/src/test/ui/const-generics/issues/issue-61336-2.stderr b/src/test/ui/const-generics/issues/issue-61336-2.stderr index 27ee4f88870b9..55b75fb08f9ed 100644 --- a/src/test/ui/const-generics/issues/issue-61336-2.stderr +++ b/src/test/ui/const-generics/issues/issue-61336-2.stderr @@ -6,6 +6,12 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-61336-2.rs:3:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied --> $DIR/issue-61336-2.rs:9:5 | diff --git a/src/test/ui/const-generics/issues/issue-61336.rs b/src/test/ui/const-generics/issues/issue-61336.rs index 7e84e62d8be42..5b46d95047c75 100644 --- a/src/test/ui/const-generics/issues/issue-61336.rs +++ b/src/test/ui/const-generics/issues/issue-61336.rs @@ -1,5 +1,7 @@ #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash fn f(x: T) -> [T; N] { [x; N] diff --git a/src/test/ui/const-generics/issues/issue-61336.stderr b/src/test/ui/const-generics/issues/issue-61336.stderr index 772a07cccf884..ef248b817cfdd 100644 --- a/src/test/ui/const-generics/issues/issue-61336.stderr +++ b/src/test/ui/const-generics/issues/issue-61336.stderr @@ -6,6 +6,12 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-61336.rs:3:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied --> $DIR/issue-61336.rs:9:5 | diff --git a/src/test/ui/const-generics/issues/issue-61747.rs b/src/test/ui/const-generics/issues/issue-61747.rs index 64674bb894e1f..cd73cbbb41960 100644 --- a/src/test/ui/const-generics/issues/issue-61747.rs +++ b/src/test/ui/const-generics/issues/issue-61747.rs @@ -2,6 +2,8 @@ #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash struct Const; diff --git a/src/test/ui/const-generics/issues/issue-61747.stderr b/src/test/ui/const-generics/issues/issue-61747.stderr index 3465db152084c..c5b3d8118a40e 100644 --- a/src/test/ui/const-generics/issues/issue-61747.stderr +++ b/src/test/ui/const-generics/issues/issue-61747.stderr @@ -6,5 +6,13 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default +<<<<<<< HEAD warning: 1 warning emitted +======= +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-61747.rs:5:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +>>>>>>> Added `lazy_normalization_consts` feature, and removed the -Z flag. diff --git a/src/test/ui/const-generics/issues/issue-61935.rs b/src/test/ui/const-generics/issues/issue-61935.rs index 5277d8ec27ec4..445862cccdd89 100644 --- a/src/test/ui/const-generics/issues/issue-61935.rs +++ b/src/test/ui/const-generics/issues/issue-61935.rs @@ -1,8 +1,9 @@ // check-pass -// compile-flags: -Z lazy-normalization #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash trait Foo {} diff --git a/src/test/ui/const-generics/issues/issue-61935.stderr b/src/test/ui/const-generics/issues/issue-61935.stderr index cc27dd6f3ae19..b35f3ad891d5b 100644 --- a/src/test/ui/const-generics/issues/issue-61935.stderr +++ b/src/test/ui/const-generics/issues/issue-61935.stderr @@ -1,8 +1,14 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-61935.rs:4:12 + --> $DIR/issue-61935.rs:3:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ | = note: `#[warn(incomplete_features)]` on by default +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-61935.rs:5:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + diff --git a/src/test/ui/const-generics/issues/issue-66205.rs b/src/test/ui/const-generics/issues/issue-66205.rs index 73ba4fa6aae88..76bde1815be18 100644 --- a/src/test/ui/const-generics/issues/issue-66205.rs +++ b/src/test/ui/const-generics/issues/issue-66205.rs @@ -1,5 +1,6 @@ #![allow(incomplete_features, dead_code, unconditional_recursion)] #![feature(const_generics)] +#![feature(lazy_normalization_consts)] fn fact() { fact::<{ N - 1 }>(); diff --git a/src/test/ui/const-generics/issues/issue-67185-1.rs b/src/test/ui/const-generics/issues/issue-67185-1.rs index a38049ed26ffb..664dbaeb06791 100644 --- a/src/test/ui/const-generics/issues/issue-67185-1.rs +++ b/src/test/ui/const-generics/issues/issue-67185-1.rs @@ -1,8 +1,9 @@ // check-pass -// compile-flags: -Z lazy-normalization #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash trait Baz { type Quaks; diff --git a/src/test/ui/const-generics/issues/issue-67185-1.stderr b/src/test/ui/const-generics/issues/issue-67185-1.stderr index 211cd1628d2f0..257949340d0f0 100644 --- a/src/test/ui/const-generics/issues/issue-67185-1.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-1.stderr @@ -1,8 +1,14 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-67185-1.rs:4:12 + --> $DIR/issue-67185-1.rs:3:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ | = note: `#[warn(incomplete_features)]` on by default +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-67185-1.rs:5:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + diff --git a/src/test/ui/const-generics/issues/issue-67185-2.rs b/src/test/ui/const-generics/issues/issue-67185-2.rs index f0f06339fe0de..52c8a5c9de809 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.rs +++ b/src/test/ui/const-generics/issues/issue-67185-2.rs @@ -1,7 +1,7 @@ -// compile-flags: -Z lazy-normalization - #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash trait Baz { type Quaks; @@ -14,7 +14,8 @@ trait Bar {} impl Bar for [u16; 4] {} impl Bar for [[u16; 3]; 3] {} -trait Foo //~ ERROR mismatched types +trait Foo //~ ERROR the trait bound `[u16; 3]: Bar` is not satisfied [E0277] + //~^ ERROR the trait bound `[[u16; 3]; 2]: Bar` is not satisfied [E0277] where [::Quaks; 2]: Bar, ::Quaks: Bar, @@ -24,12 +25,12 @@ trait Foo //~ ERROR mismatched types struct FooImpl; impl Foo for FooImpl {} -//~^ ERROR mismatched types -//~^^ ERROR mismatched types +//~^ ERROR the trait bound `[u16; 3]: Bar` is not satisfied [E0277] +//~^^ ERROR the trait bound `[[u16; 3]; 2]: Bar` is not satisfied [E0277] fn f(_: impl Foo) {} -//~^ ERROR mismatched types -//~^^ ERROR mismatched types +//~^ ERROR the trait bound `[u16; 3]: Bar` is not satisfied [E0277] +//~^^ ERROR the trait bound `[[u16; 3]; 2]: Bar` is not satisfied [E0277] fn main() { f(FooImpl) diff --git a/src/test/ui/const-generics/issues/issue-67185-2.stderr b/src/test/ui/const-generics/issues/issue-67185-2.stderr index 215093364c6d6..c8620fc268ffb 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-2.stderr @@ -1,61 +1,111 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-67185-2.rs:3:12 + --> $DIR/issue-67185-2.rs:1:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ | = note: `#[warn(incomplete_features)]` on by default -error[E0308]: mismatched types +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/issue-67185-2.rs:3:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied + --> $DIR/issue-67185-2.rs:17:1 + | +LL | / trait Foo +LL | | +LL | | where +LL | | [::Quaks; 2]: Bar, +LL | | ::Quaks: Bar, +LL | | { +LL | | } + | |_^ the trait `Bar` is not implemented for `[u16; 3]` + | + = help: the following implementations were found: + <[[u16; 3]; 3] as Bar> + <[u16; 4] as Bar> + = help: see issue #48214 + = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable + +error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied --> $DIR/issue-67185-2.rs:17:1 | LL | / trait Foo +LL | | LL | | where LL | | [::Quaks; 2]: Bar, LL | | ::Quaks: Bar, LL | | { LL | | } - | |_^ expected `3usize`, found `4usize` + | |_^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` | - = note: expected type `3usize` - found type `4usize` + = help: the following implementations were found: + <[[u16; 3]; 3] as Bar> + <[u16; 4] as Bar> + = help: see issue #48214 + = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable -error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:26:6 +error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied + --> $DIR/issue-67185-2.rs:27:6 | LL | impl Foo for FooImpl {} - | ^^^ expected `3usize`, found `4usize` + | ^^^ the trait `Bar` is not implemented for `[u16; 3]` | - = note: expected type `3usize` - found type `4usize` + = help: the following implementations were found: + <[[u16; 3]; 3] as Bar> + <[u16; 4] as Bar> -error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:26:6 +error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied + --> $DIR/issue-67185-2.rs:27:6 | LL | impl Foo for FooImpl {} - | ^^^ expected `2usize`, found `3usize` + | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` | - = note: expected type `2usize` - found type `3usize` + = help: the following implementations were found: + <[[u16; 3]; 3] as Bar> + <[u16; 4] as Bar> -error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:30:1 +error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied + --> $DIR/issue-67185-2.rs:31:14 | -LL | fn f(_: impl Foo) {} - | ^^^^^^^^^^^^^^^^^^^^ expected `2usize`, found `3usize` +LL | / trait Foo +LL | | +LL | | where +LL | | [::Quaks; 2]: Bar, +LL | | ::Quaks: Bar, +LL | | { +LL | | } + | |_- required by `Foo` +... +LL | fn f(_: impl Foo) {} + | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` | - = note: expected type `2usize` - found type `3usize` + = help: the following implementations were found: + <[[u16; 3]; 3] as Bar> + <[u16; 4] as Bar> -error[E0308]: mismatched types - --> $DIR/issue-67185-2.rs:30:1 +error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied + --> $DIR/issue-67185-2.rs:31:14 | -LL | fn f(_: impl Foo) {} - | ^^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `4usize` +LL | / trait Foo +LL | | +LL | | where +LL | | [::Quaks; 2]: Bar, +LL | | ::Quaks: Bar, +LL | | { +LL | | } + | |_- required by `Foo` +... +LL | fn f(_: impl Foo) {} + | ^^^ the trait `Bar` is not implemented for `[u16; 3]` | - = note: expected type `3usize` - found type `4usize` + = help: the following implementations were found: + <[[u16; 3]; 3] as Bar> + <[u16; 4] as Bar> -error: aborting due to 5 previous errors +error: aborting due to 6 previous errors -For more information about this error, try `rustc --explain E0308`. +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/const-generics/trait-const-args.rs b/src/test/ui/const-generics/trait-const-args.rs index 7a5901069cdef..2bb5514d8d71f 100644 --- a/src/test/ui/const-generics/trait-const-args.rs +++ b/src/test/ui/const-generics/trait-const-args.rs @@ -1,8 +1,9 @@ // check-pass -// compile-flags: -Z lazy-normalization #![feature(const_generics)] //~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +#![feature(lazy_normalization_consts)] +//~^ WARN the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash struct Const; trait Foo {} diff --git a/src/test/ui/const-generics/trait-const-args.stderr b/src/test/ui/const-generics/trait-const-args.stderr index 1e7c84fdbac99..617b6ad45f7c6 100644 --- a/src/test/ui/const-generics/trait-const-args.stderr +++ b/src/test/ui/const-generics/trait-const-args.stderr @@ -1,8 +1,14 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/trait-const-args.rs:4:12 + --> $DIR/trait-const-args.rs:3:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ | = note: `#[warn(incomplete_features)]` on by default +warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash + --> $DIR/trait-const-args.rs:5:12 + | +LL | #![feature(lazy_normalization_consts)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + From b1abe0a661e74d4b273d046ddef0d494047e4700 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 17:57:23 +0200 Subject: [PATCH 07/17] fmt --- src/librustc_trait_selection/traits/error_reporting/mod.rs | 1 - src/librustc_trait_selection/traits/fulfill.rs | 5 ++++- src/librustc_trait_selection/traits/object_safety.rs | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 1737d505367ec..1b8b1fd9fb3cb 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -590,7 +590,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ) } - ty::Predicate::ConstEquate(..) => { // Errors for `ConstEquate` predicates show up as // `SelectionError::ConstEvalFailure`, diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index 0c3b3252c66fe..e8e8d732756e1 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -531,7 +531,10 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)), Err(ErrorHandled::TooGeneric) => { stalled_on.append( - &mut substs.types().filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty)).collect(), + &mut substs + .types() + .filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty)) + .collect(), ); Err(ProcessResult::Unchanged) } diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index 3b4533b04ba4c..04ac7ac91e708 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -17,7 +17,9 @@ use rustc_errors::{Applicability, FatalError}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_middle::ty::subst::{GenericArg, InternalSubsts, Subst}; -use rustc_middle::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitor, WithConstness}; +use rustc_middle::ty::{ + self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitor, WithConstness, +}; use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY; use rustc_span::symbol::Symbol; use rustc_span::Span; From c112b0a199a55b05a174b26131ea1f14a500f79e Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 18:00:53 +0200 Subject: [PATCH 08/17] fix `DefId` mismatch --- src/librustc_typeck/collect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index efd185d5f7f0e..4c9f47941e1b4 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1168,7 +1168,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { Node::AnonConst(_) => { if tcx.features().lazy_normalization_consts { let parent_id = tcx.hir().get_parent_item(hir_id); - Some(tcx.hir().local_def_id(parent_id)) + Some(tcx.hir().local_def_id(parent_id).to_def_id()) } else { None } From 55293f69ae393c39f6092625ee4dd0fb4c009090 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 18:11:39 +0200 Subject: [PATCH 09/17] remove spurious changes 1 --- src/librustc_infer/infer/nll_relate/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_infer/infer/nll_relate/mod.rs b/src/librustc_infer/infer/nll_relate/mod.rs index f1a4b2adcb741..4cc7ec998a668 100644 --- a/src/librustc_infer/infer/nll_relate/mod.rs +++ b/src/librustc_infer/infer/nll_relate/mod.rs @@ -595,8 +595,8 @@ where b = self.infcx.shallow_resolve(b); } - match (a.val, b.val) { - (_, ty::ConstKind::Infer(InferConst::Var(_))) if D::forbid_inference_vars() => { + match b.val { + ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { // Forbid inference variables in the RHS. bug!("unexpected inference var {:?}", b) } From bad9174989509754c38d692d59206814b09bca22 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 22:43:15 +0200 Subject: [PATCH 10/17] add missing super folds --- src/librustc_trait_selection/traits/project.rs | 1 + src/librustc_trait_selection/traits/query/normalize.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index 8bf45d8fc8e82..24be88d085f12 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -390,6 +390,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { if self.selcx.tcx().features().lazy_normalization_consts { constant } else { + let constant = constant.super_fold_with(self); constant.eval(self.selcx.tcx(), self.param_env) } } diff --git a/src/librustc_trait_selection/traits/query/normalize.rs b/src/librustc_trait_selection/traits/query/normalize.rs index 7b620202abf64..0da26abc330ac 100644 --- a/src/librustc_trait_selection/traits/query/normalize.rs +++ b/src/librustc_trait_selection/traits/query/normalize.rs @@ -202,6 +202,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> { } fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + let constant = constant.super_fold_with(self); constant.eval(self.infcx.tcx, self.param_env) } } From 5426b7adf018c6c0671cfc3e0dc844ac7666c0cd Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 22:47:06 +0200 Subject: [PATCH 11/17] tidy --- src/librustc_feature/active.rs | 2 +- src/librustc_trait_selection/traits/object_safety.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/librustc_feature/active.rs b/src/librustc_feature/active.rs index f075988099b9e..894b392f1c036 100644 --- a/src/librustc_feature/active.rs +++ b/src/librustc_feature/active.rs @@ -558,7 +558,7 @@ declare_features! ( /// Allow negative trait implementations. (active, negative_impls, "1.44.0", Some(68318), None), - + /// Lazily evaluate constants. Which allows constants to depend on type parameters. (active, lazy_normalization_consts, "1.44.0", Some(60471), None), diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index 04ac7ac91e708..a34006dd4fc06 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -17,9 +17,8 @@ use rustc_errors::{Applicability, FatalError}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_middle::ty::subst::{GenericArg, InternalSubsts, Subst}; -use rustc_middle::ty::{ - self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitor, WithConstness, -}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor, WithConstness}; +use rustc_middle::ty::{Predicate, ToPredicate}; use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY; use rustc_span::symbol::Symbol; use rustc_span::Span; From 18e67bd374c6df89f5e88866ac48f8e138da7b2e Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 22:47:47 +0200 Subject: [PATCH 12/17] keep the good old lazy_normalization hack alive --- src/librustc_typeck/collect.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 4c9f47941e1b4..ab97ede90a03b 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1164,13 +1164,33 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { let parent_id = tcx.hir().get_parent_item(hir_id); Some(tcx.hir().local_def_id(parent_id).to_def_id()) } - + // FIXME(#43408) always enable this once we use `lazy_normalization` is + // stable enough and does not need a feature gate anymore. Node::AnonConst(_) => { - if tcx.features().lazy_normalization_consts { - let parent_id = tcx.hir().get_parent_item(hir_id); - Some(tcx.hir().local_def_id(parent_id).to_def_id()) + let parent_id = tcx.hir().get_parent_item(hir_id); + let parent_def_id = tcx.hir().local_def_id(parent_id); + + // HACK(eddyb) this provides the correct generics when + // `feature(const_generics)` is enabled, so that const expressions + // used with const generics, e.g. `Foo<{N+1}>`, can work at all. + if tcx.features().const_generics || tcx.features().lazy_normalization_consts { + Some(parent_def_id.to_def_id()) } else { - None + let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id)); + match parent_node { + // HACK(eddyb) this provides the correct generics for repeat + // expressions' count (i.e. `N` in `[x; N]`), and explicit + // `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`), + // as they shouldn't be able to cause query cycle errors. + Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. }) + | Node::Variant(Variant { disr_expr: Some(ref constant), .. }) + if constant.hir_id == hir_id => + { + Some(parent_def_id.to_def_id()) + } + + _ => None, + } } } From 072f453f38ddf42a80c360ec864170ca808601a4 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 22:47:54 +0200 Subject: [PATCH 13/17] test me --- .../array-size-in-generic-struct-param.stderr | 2 +- .../issues/issue-61336-1.stderr | 8 ++-- .../issues/issue-61336-2.stderr | 4 +- .../const-generics/issues/issue-61336.stderr | 4 +- .../const-generics/issues/issue-61747.stderr | 6 +-- .../const-generics/issues/issue-61935.stderr | 2 + .../const-generics/issues/issue-66205.stderr | 2 +- .../issues/issue-67185-1.stderr | 2 + .../issues/issue-67185-2.stderr | 48 +++++++++++-------- .../ui/const-generics/trait-const-args.stderr | 2 + 10 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr b/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr index cd4526d834981..31963a7beb1e9 100644 --- a/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr +++ b/src/test/ui/const-generics/array-size-in-generic-struct-param.stderr @@ -28,5 +28,5 @@ LL | arr: [u8; CFG.arr_size], | = note: this may fail depending on what value the parameter takes -error: aborting due to 2 previous errors; 1 warning emitted +error: aborting due to 2 previous errors; 2 warnings emitted diff --git a/src/test/ui/const-generics/issues/issue-61336-1.stderr b/src/test/ui/const-generics/issues/issue-61336-1.stderr index aa802b75683b8..7a38ea722bc22 100644 --- a/src/test/ui/const-generics/issues/issue-61336-1.stderr +++ b/src/test/ui/const-generics/issues/issue-61336-1.stderr @@ -6,13 +6,11 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default -<<<<<<< HEAD -warning: 1 warning emitted - -======= warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash --> $DIR/issue-61336-1.rs:3:12 | LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ ->>>>>>> Added `lazy_normalization_consts` feature, and removed the -Z flag. + +warning: 2 warnings emitted + diff --git a/src/test/ui/const-generics/issues/issue-61336-2.stderr b/src/test/ui/const-generics/issues/issue-61336-2.stderr index 55b75fb08f9ed..ad0a791c31e09 100644 --- a/src/test/ui/const-generics/issues/issue-61336-2.stderr +++ b/src/test/ui/const-generics/issues/issue-61336-2.stderr @@ -13,7 +13,7 @@ LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied - --> $DIR/issue-61336-2.rs:9:5 + --> $DIR/issue-61336-2.rs:11:5 | LL | [x; { N }] | ^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `T` @@ -24,6 +24,6 @@ help: consider restricting type parameter `T` LL | fn g(x: T) -> [T; N] { | ^^^^^^^^^^^^^^^^^^^ -error: aborting due to previous error; 1 warning emitted +error: aborting due to previous error; 2 warnings emitted For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/const-generics/issues/issue-61336.stderr b/src/test/ui/const-generics/issues/issue-61336.stderr index ef248b817cfdd..c1f2d4122339d 100644 --- a/src/test/ui/const-generics/issues/issue-61336.stderr +++ b/src/test/ui/const-generics/issues/issue-61336.stderr @@ -13,7 +13,7 @@ LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied - --> $DIR/issue-61336.rs:9:5 + --> $DIR/issue-61336.rs:11:5 | LL | [x; N] | ^^^^^^ the trait `std::marker::Copy` is not implemented for `T` @@ -24,6 +24,6 @@ help: consider restricting type parameter `T` LL | fn g(x: T) -> [T; N] { | ^^^^^^^^^^^^^^^^^^^ -error: aborting due to previous error; 1 warning emitted +error: aborting due to previous error; 2 warnings emitted For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/const-generics/issues/issue-61747.stderr b/src/test/ui/const-generics/issues/issue-61747.stderr index c5b3d8118a40e..acfcc9c521ec6 100644 --- a/src/test/ui/const-generics/issues/issue-61747.stderr +++ b/src/test/ui/const-generics/issues/issue-61747.stderr @@ -6,13 +6,11 @@ LL | #![feature(const_generics)] | = note: `#[warn(incomplete_features)]` on by default -<<<<<<< HEAD -warning: 1 warning emitted -======= warning: the feature `lazy_normalization_consts` is incomplete and may cause the compiler to crash --> $DIR/issue-61747.rs:5:12 | LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ ->>>>>>> Added `lazy_normalization_consts` feature, and removed the -Z flag. + +warning: 2 warnings emitted diff --git a/src/test/ui/const-generics/issues/issue-61935.stderr b/src/test/ui/const-generics/issues/issue-61935.stderr index b35f3ad891d5b..c74c2ba67dde2 100644 --- a/src/test/ui/const-generics/issues/issue-61935.stderr +++ b/src/test/ui/const-generics/issues/issue-61935.stderr @@ -12,3 +12,5 @@ warning: the feature `lazy_normalization_consts` is incomplete and may cause the LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: 2 warnings emitted + diff --git a/src/test/ui/const-generics/issues/issue-66205.stderr b/src/test/ui/const-generics/issues/issue-66205.stderr index 2bd013e8b41f2..416b675b56d28 100644 --- a/src/test/ui/const-generics/issues/issue-66205.stderr +++ b/src/test/ui/const-generics/issues/issue-66205.stderr @@ -1,5 +1,5 @@ error: constant expression depends on a generic parameter - --> $DIR/issue-66205.rs:5:12 + --> $DIR/issue-66205.rs:6:12 | LL | fact::<{ N - 1 }>(); | ^^^^^^^^^ diff --git a/src/test/ui/const-generics/issues/issue-67185-1.stderr b/src/test/ui/const-generics/issues/issue-67185-1.stderr index 257949340d0f0..2472693afcbaf 100644 --- a/src/test/ui/const-generics/issues/issue-67185-1.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-1.stderr @@ -12,3 +12,5 @@ warning: the feature `lazy_normalization_consts` is incomplete and may cause the LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: 2 warnings emitted + diff --git a/src/test/ui/const-generics/issues/issue-67185-2.stderr b/src/test/ui/const-generics/issues/issue-67185-2.stderr index c8620fc268ffb..76d2622e9af0f 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-2.stderr @@ -51,6 +51,12 @@ LL | | } error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied --> $DIR/issue-67185-2.rs:27:6 | +LL | trait Foo + | --- required by a bound in this +... +LL | ::Quaks: Bar, + | --- required by this bound in `Foo` +... LL | impl Foo for FooImpl {} | ^^^ the trait `Bar` is not implemented for `[u16; 3]` | @@ -61,6 +67,12 @@ LL | impl Foo for FooImpl {} error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied --> $DIR/issue-67185-2.rs:27:6 | +LL | trait Foo + | --- required by a bound in this +... +LL | [::Quaks; 2]: Bar, + | --- required by this bound in `Foo` +... LL | impl Foo for FooImpl {} | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` | @@ -71,17 +83,14 @@ LL | impl Foo for FooImpl {} error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied --> $DIR/issue-67185-2.rs:31:14 | -LL | / trait Foo -LL | | -LL | | where -LL | | [::Quaks; 2]: Bar, -LL | | ::Quaks: Bar, -LL | | { -LL | | } - | |_- required by `Foo` +LL | trait Foo + | --- required by a bound in this +... +LL | [::Quaks; 2]: Bar, + | --- required by this bound in `Foo` ... -LL | fn f(_: impl Foo) {} - | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` +LL | fn f(_: impl Foo) {} + | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` | = help: the following implementations were found: <[[u16; 3]; 3] as Bar> @@ -90,22 +99,19 @@ LL | fn f(_: impl Foo) {} error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied --> $DIR/issue-67185-2.rs:31:14 | -LL | / trait Foo -LL | | -LL | | where -LL | | [::Quaks; 2]: Bar, -LL | | ::Quaks: Bar, -LL | | { -LL | | } - | |_- required by `Foo` +LL | trait Foo + | --- required by a bound in this +... +LL | ::Quaks: Bar, + | --- required by this bound in `Foo` ... -LL | fn f(_: impl Foo) {} - | ^^^ the trait `Bar` is not implemented for `[u16; 3]` +LL | fn f(_: impl Foo) {} + | ^^^ the trait `Bar` is not implemented for `[u16; 3]` | = help: the following implementations were found: <[[u16; 3]; 3] as Bar> <[u16; 4] as Bar> -error: aborting due to 6 previous errors +error: aborting due to 6 previous errors; 2 warnings emitted For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/const-generics/trait-const-args.stderr b/src/test/ui/const-generics/trait-const-args.stderr index 617b6ad45f7c6..ff88a55b43ead 100644 --- a/src/test/ui/const-generics/trait-const-args.stderr +++ b/src/test/ui/const-generics/trait-const-args.stderr @@ -12,3 +12,5 @@ warning: the feature `lazy_normalization_consts` is incomplete and may cause the LL | #![feature(lazy_normalization_consts)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: 2 warnings emitted + From c777a45c7b0e11405a9bcc8cfc074bdc0769d9c9 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 23:03:13 +0200 Subject: [PATCH 14/17] feature gate hack --- .../lazy-normalization-feature-gate-hack.rs | 8 ++++++++ .../lazy-normalization-feature-gate-hack.stderr | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs create mode 100644 src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr diff --git a/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs b/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs new file mode 100644 index 0000000000000..ec42634bde518 --- /dev/null +++ b/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs @@ -0,0 +1,8 @@ +// gate-test-lazy_normalization_consts + +// FIXME(lazy-normalization) We currently don't have a good way to +// test if `#![feature(lazy_normalization_consts)]` features is missing. +// +// Once lazy normalization is more powerful we should definitely replace this +// with an actually relevant test. (#60471) +fn //~ ERROR expected identifier diff --git a/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr b/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr new file mode 100644 index 0000000000000..65c30115db691 --- /dev/null +++ b/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr @@ -0,0 +1,8 @@ +error: expected identifier, found `` + --> $DIR/lazy-normalization-feature-gate-hack.rs:8:1 + | +LL | fn + | ^^ expected identifier + +error: aborting due to previous error + From 05d1c199dbaf321ec40c979822f4820c2c4fe683 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 23:03:32 +0200 Subject: [PATCH 15/17] words --- src/librustc_typeck/check/mod.rs | 2 +- src/librustc_typeck/collect.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index d336b6940467f..7941434ede9ba 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1658,7 +1658,7 @@ fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: LocalDefId, fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool { if let ty::ConstKind::Unevaluated(..) = c.val { - // FIXME This check detect lifetimes within substs which violates this check even + // FIXME This check detects lifetimes within substs which violates this check even // though the particular substitution is not used within the const. return false; } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index ab97ede90a03b..2a7acfd888961 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1164,7 +1164,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { let parent_id = tcx.hir().get_parent_item(hir_id); Some(tcx.hir().local_def_id(parent_id).to_def_id()) } - // FIXME(#43408) always enable this once we use `lazy_normalization` is + // FIXME(#43408) always enable this once `lazy_normalization` is // stable enough and does not need a feature gate anymore. Node::AnonConst(_) => { let parent_id = tcx.hir().get_parent_item(hir_id); @@ -1193,7 +1193,6 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { } } } - Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { Some(tcx.closure_base_def_id(def_id)) } From 1ed8202572ebd690b59cac4ff06a169cbbb99809 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 23:42:16 +0200 Subject: [PATCH 16/17] add actually relevant issue --- .../lazy-normalization-feature-gate-hack.rs | 8 ---- ...azy-normalization-feature-gate-hack.stderr | 8 ---- .../lazy-normalization/issue-71922.rs | 20 +++++++++ .../lazy-normalization-feature-gate-hack.rs | 20 +++++++++ ...azy-normalization-feature-gate-hack.stderr | 44 +++++++++++++++++++ 5 files changed, 84 insertions(+), 16 deletions(-) delete mode 100644 src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs delete mode 100644 src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr create mode 100644 src/test/ui/const-generics/lazy-normalization/issue-71922.rs create mode 100644 src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.rs create mode 100644 src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.stderr diff --git a/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs b/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs deleted file mode 100644 index ec42634bde518..0000000000000 --- a/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.rs +++ /dev/null @@ -1,8 +0,0 @@ -// gate-test-lazy_normalization_consts - -// FIXME(lazy-normalization) We currently don't have a good way to -// test if `#![feature(lazy_normalization_consts)]` features is missing. -// -// Once lazy normalization is more powerful we should definitely replace this -// with an actually relevant test. (#60471) -fn //~ ERROR expected identifier diff --git a/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr b/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr deleted file mode 100644 index 65c30115db691..0000000000000 --- a/src/test/ui/const-generics/lazy-normalization-feature-gate-hack.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: expected identifier, found `` - --> $DIR/lazy-normalization-feature-gate-hack.rs:8:1 - | -LL | fn - | ^^ expected identifier - -error: aborting due to previous error - diff --git a/src/test/ui/const-generics/lazy-normalization/issue-71922.rs b/src/test/ui/const-generics/lazy-normalization/issue-71922.rs new file mode 100644 index 0000000000000..60597b8be620d --- /dev/null +++ b/src/test/ui/const-generics/lazy-normalization/issue-71922.rs @@ -0,0 +1,20 @@ +// run-pass +#![feature(const_generics)] +#![feature(lazy_normalization_consts)] +#![allow(incomplete_features)] +trait Foo {} + +impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + +trait FooImpl {} + +impl FooImpl<{ 0u8 == 0u8 }> for [(); 0] {} + +impl FooImpl<{ 0u8 != 0u8 }> for [(); N] {} + +fn foo(_: T) {} + +fn main() { + foo([]); + foo([()]); +} diff --git a/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.rs b/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.rs new file mode 100644 index 0000000000000..2c6fbfe65d6e1 --- /dev/null +++ b/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.rs @@ -0,0 +1,20 @@ +// gate-test-lazy_normalization_consts +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash +trait Foo {} + +impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} +//~^ ERROR cycle detected + +trait FooImpl {} + +impl FooImpl<{ 0u8 == 0u8 }> for [(); 0] {} + +impl FooImpl<{ 0u8 != 0u8 }> for [(); N] {} + +fn foo(_: T) {} + +fn main() { + foo([]); + foo([()]); +} diff --git a/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.stderr b/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.stderr new file mode 100644 index 0000000000000..8a97ec0c5fc88 --- /dev/null +++ b/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.stderr @@ -0,0 +1,44 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/lazy-normalization-feature-gate-hack.rs:2:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + +error[E0391]: cycle detected when const-evaluating + checking `::{{constant}}#0` + --> $DIR/lazy-normalization-feature-gate-hack.rs:6:58 + | +LL | impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + | ^^^^^^^^^^ + | +note: ...which requires const-evaluating + checking `::{{constant}}#0`... + --> $DIR/lazy-normalization-feature-gate-hack.rs:6:58 + | +LL | impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + | ^^^^^^^^^^ +note: ...which requires const-evaluating `::{{constant}}#0`... + --> $DIR/lazy-normalization-feature-gate-hack.rs:6:58 + | +LL | impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + | ^^^^^^^^^^ +note: ...which requires type-checking `::{{constant}}#0`... + --> $DIR/lazy-normalization-feature-gate-hack.rs:6:58 + | +LL | impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + | ^^^^^^^^^^ +note: ...which requires processing `::{{constant}}#0`... + --> $DIR/lazy-normalization-feature-gate-hack.rs:6:58 + | +LL | impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + | ^^^^^^^^^^ + = note: ...which again requires const-evaluating + checking `::{{constant}}#0`, completing the cycle +note: cycle used when processing `` + --> $DIR/lazy-normalization-feature-gate-hack.rs:6:1 + | +LL | impl Foo for [(); N] where Self: FooImpl<{ N == 0 }> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0391`. From a1c225919425846f147a58e36152c99ac23a0ce2 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 5 May 2020 23:45:19 +0200 Subject: [PATCH 17/17] rename feature gate test --- ...on-feature-gate-hack.rs => lazy-normalization-feature-gate.rs} | 0 ...re-gate-hack.stderr => lazy-normalization-feature-gate.stderr} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/test/ui/const-generics/lazy-normalization/{lazy-normalization-feature-gate-hack.rs => lazy-normalization-feature-gate.rs} (100%) rename src/test/ui/const-generics/lazy-normalization/{lazy-normalization-feature-gate-hack.stderr => lazy-normalization-feature-gate.stderr} (100%) diff --git a/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.rs b/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate.rs similarity index 100% rename from src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.rs rename to src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate.rs diff --git a/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.stderr b/src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate.stderr similarity index 100% rename from src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate-hack.stderr rename to src/test/ui/const-generics/lazy-normalization/lazy-normalization-feature-gate.stderr