From b519deb224cf94f61599ba0626e2ef3db22826f3 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 27 Jan 2021 13:28:52 +0100 Subject: [PATCH] const_evaluatable: stop looking into type aliases --- compiler/rustc_typeck/src/collect.rs | 34 ------------------- .../simple_fail.full.stderr | 8 ++++- .../simple_fail.min.stderr | 11 +++++- .../const_evaluatable_checked/simple_fail.rs | 6 ++-- .../ty-alias-substitution.rs | 14 ++++++++ 5 files changed, 35 insertions(+), 38 deletions(-) create mode 100644 src/test/ui/const-generics/const_evaluatable_checked/ty-alias-substitution.rs diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index d589989511db1..b1d98d75196d5 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -50,8 +50,6 @@ use rustc_span::{Span, DUMMY_SP}; use rustc_target::spec::abi; use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName; -use std::ops::ControlFlow; - mod item_bounds; mod type_of; @@ -2080,38 +2078,6 @@ fn const_evaluatable_predicates_of<'tcx>( )); } } - - // Look into `TyAlias`. - fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) { - use ty::fold::{TypeFoldable, TypeVisitor}; - struct TyAliasVisitor<'a, 'tcx> { - tcx: TyCtxt<'tcx>, - preds: &'a mut FxIndexSet<(ty::Predicate<'tcx>, Span)>, - span: Span, - } - - impl<'a, 'tcx> TypeVisitor<'tcx> for TyAliasVisitor<'a, 'tcx> { - fn visit_const(&mut self, ct: &'tcx Const<'tcx>) -> ControlFlow { - if let ty::ConstKind::Unevaluated(def, substs, None) = ct.val { - self.preds.insert(( - ty::PredicateKind::ConstEvaluatable(def, substs).to_predicate(self.tcx), - self.span, - )); - } - ControlFlow::CONTINUE - } - } - - if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ty.kind { - if let Res::Def(DefKind::TyAlias, def_id) = path.res { - let mut visitor = - TyAliasVisitor { tcx: self.tcx, preds: &mut self.preds, span: path.span }; - self.tcx.type_of(def_id).visit_with(&mut visitor); - } - } - - intravisit::walk_ty(self, ty) - } } let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr index c8549f101daf6..acf0a52ce5be1 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr +++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr @@ -1,9 +1,15 @@ +error[E0080]: evaluation of constant value failed + --> $DIR/simple_fail.rs:9:48 + | +LL | fn test() -> Arr where [u8; N - 1]: Sized { + | ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow + error[E0080]: evaluation of constant value failed --> $DIR/simple_fail.rs:6:33 | LL | type Arr = [u8; N - 1]; | ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow -error: aborting due to previous error +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr index df54b4cbca5bd..fe5463f8acc4a 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr +++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr @@ -7,5 +7,14 @@ LL | type Arr = [u8; N - 1]; = help: const parameters may only be used as standalone arguments, i.e. `N` = help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions -error: aborting due to previous error +error: generic parameters may not be used in const operations + --> $DIR/simple_fail.rs:9:48 + | +LL | fn test() -> Arr where [u8; N - 1]: Sized { + | ^ cannot perform const operation using `N` + | + = help: const parameters may only be used as standalone arguments, i.e. `N` + = help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions + +error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs index 3cbc077f4f146..c9535d04244d8 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs +++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs @@ -1,12 +1,14 @@ // revisions: full min #![cfg_attr(full, feature(const_generics))] -#![feature(const_evaluatable_checked)] +#![cfg_attr(full, feature(const_evaluatable_checked))] #![allow(incomplete_features)] type Arr = [u8; N - 1]; //[full]~ ERROR evaluation of constant //[min]~^ ERROR generic parameters may not be used in const operations -fn test() -> Arr where Arr: Sized { +fn test() -> Arr where [u8; N - 1]: Sized { +//[min]~^ ERROR generic parameters may not be used in const operations +//[full]~^^ ERROR evaluation of constant todo!() } diff --git a/src/test/ui/const-generics/const_evaluatable_checked/ty-alias-substitution.rs b/src/test/ui/const-generics/const_evaluatable_checked/ty-alias-substitution.rs new file mode 100644 index 0000000000000..5c768a61be25e --- /dev/null +++ b/src/test/ui/const-generics/const_evaluatable_checked/ty-alias-substitution.rs @@ -0,0 +1,14 @@ +// check-pass +// Test that we correctly substitute generic arguments for type aliases. +#![feature(const_generics, const_evaluatable_checked)] +#![allow(incomplete_features)] + +type Alias = [T; N + 1]; + +fn foo() -> Alias where [u8; M + 1]: Sized { + [0; M + 1] +} + +fn main() { + foo::<0>(); +}