From ff448cfcee92cf507488d6335c68ca98a70cf7cc Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 26 Nov 2021 17:41:22 +0100 Subject: [PATCH 1/7] implement version of normalize_erasing_regions that doesn't assume value is normalizable --- compiler/rustc_lint/src/types.rs | 4 +- .../rustc_middle/src/mir/interpret/error.rs | 2 +- compiler/rustc_middle/src/query/mod.rs | 14 ++ compiler/rustc_middle/src/ty/layout.rs | 19 ++- .../src/ty/normalize_erasing_regions.rs | 127 ++++++++++++++++++ .../src/normalize_erasing_regions.rs | 40 ++++++ 6 files changed, 203 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 708cd56e068b5..b20f7357b35b8 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -1337,7 +1337,9 @@ impl<'tcx> LateLintPass<'tcx> for VariantSizeDifferences { let layout = match cx.layout_of(ty) { Ok(layout) => layout, Err( - ty::layout::LayoutError::Unknown(_) | ty::layout::LayoutError::SizeOverflow(_), + ty::layout::LayoutError::Unknown(_) + | ty::layout::LayoutError::SizeOverflow(_) + | ty::layout::LayoutError::NormalizationFailure(_, _), ) => return, }; let (variants, tag) = match layout.variants { diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 7a51bb4a1f32a..3e307979f2d0a 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -493,7 +493,7 @@ impl dyn MachineStopType { } #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(InterpError<'_>, 64); +static_assert_size!(InterpError<'_>, 88); pub enum InterpError<'tcx> { /// The program caused undefined behavior. diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 268a66b99269b..12665be335492 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1658,6 +1658,20 @@ rustc_queries! { desc { "normalizing `{}`", goal.value } } + /// Do not call this query directly: invoke `try_normalize_erasing_regions` instead. + query try_normalize_generic_arg_after_erasing_regions( + goal: ParamEnvAnd<'tcx, GenericArg<'tcx>> + ) -> Result, NoSolution> { + desc { "trying to normalize `{}`", goal.value } + } + + /// Do not call this query directly: invoke `try_normalize_erasing_regions` instead. + query try_normalize_mir_const_after_erasing_regions( + goal: ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>> + ) -> Result, NoSolution> { + desc { "trying to normalize `{}`", goal.value } + } + query implied_outlives_bounds( goal: CanonicalTyGoal<'tcx> ) -> Result< diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index b87e23af72b70..9c1407de812f5 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -1,5 +1,6 @@ use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; use crate::mir::{GeneratorLayout, GeneratorSavedLocal}; +use crate::ty::normalize_erasing_regions::NormalizationError; use crate::ty::subst::Subst; use crate::ty::{self, subst::SubstsRef, ReprOptions, Ty, TyCtxt, TypeFoldable}; use rustc_ast as ast; @@ -199,6 +200,7 @@ pub const MAX_SIMD_LANES: u64 = 1 << 0xF; pub enum LayoutError<'tcx> { Unknown(Ty<'tcx>), SizeOverflow(Ty<'tcx>), + NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>), } impl<'tcx> fmt::Display for LayoutError<'tcx> { @@ -208,16 +210,24 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> { LayoutError::SizeOverflow(ty) => { write!(f, "values of the type `{}` are too big for the current architecture", ty) } + LayoutError::NormalizationFailure(t, e) => write!( + f, + "unable to determine layout for `{}` because `{}` cannot be normalized", + t, + e.get_type_for_failure() + ), } } } +#[instrument(skip(tcx, query), level = "debug")] fn layout_of<'tcx>( tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>, ) -> Result, LayoutError<'tcx>> { ty::tls::with_related_context(tcx, move |icx| { let (param_env, ty) = query.into_parts(); + debug!(?ty); if !tcx.recursion_limit().value_within_limit(icx.layout_depth) { tcx.sess.fatal(&format!("overflow representing the type `{}`", ty)); @@ -229,7 +239,14 @@ fn layout_of<'tcx>( ty::tls::enter_context(&icx, |_| { let param_env = param_env.with_reveal_all_normalized(tcx); let unnormalized_ty = ty; - let ty = tcx.normalize_erasing_regions(param_env, ty); + + let ty = match tcx.try_normalize_erasing_regions(param_env, ty) { + Ok(t) => t, + Err(normalization_error) => { + return Err(LayoutError::NormalizationFailure(ty, normalization_error)); + } + }; + if ty != unnormalized_ty { // Ensure this layout is also cached for the normalized type. return tcx.layout_of(param_env.and(ty)); diff --git a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs index e6f67adae93da..0915228d07022 100644 --- a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs +++ b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs @@ -8,10 +8,28 @@ //! or constant found within. (This underlying query is what is cached.) use crate::mir; +use crate::traits::query::NoSolution; use crate::ty::fold::{TypeFoldable, TypeFolder}; use crate::ty::subst::{Subst, SubstsRef}; use crate::ty::{self, Ty, TyCtxt}; +#[derive(Debug, Copy, Clone, HashStable, TyEncodable, TyDecodable)] +pub enum NormalizationError<'tcx> { + Type(Ty<'tcx>), + Const(ty::Const<'tcx>), + ConstantKind(mir::ConstantKind<'tcx>), +} + +impl<'tcx> NormalizationError<'tcx> { + pub fn get_type_for_failure(&self) -> String { + match self { + NormalizationError::Type(t) => format!("{}", t), + NormalizationError::Const(c) => format!("{}", c), + NormalizationError::ConstantKind(ck) => format!("{}", ck), + } + } +} + impl<'tcx> TyCtxt<'tcx> { /// Erase the regions in `value` and then fully normalize all the /// types found within. The result will also have regions erased. @@ -32,6 +50,8 @@ impl<'tcx> TyCtxt<'tcx> { // Erase first before we do the real query -- this keeps the // cache from being too polluted. let value = self.erase_regions(value); + debug!(?value); + if !value.has_projections() { value } else { @@ -41,6 +61,44 @@ impl<'tcx> TyCtxt<'tcx> { } } + /// Tries to erase the regions in `value` and then fully normalize all the + /// types found within. The result will also have regions erased. + /// + /// Contrary to `normalize_erasing_regions` this function does not assume that normalization + /// succeeds. + pub fn try_normalize_erasing_regions( + self, + param_env: ty::ParamEnv<'tcx>, + value: T, + ) -> Result> + where + T: TypeFoldable<'tcx>, + { + debug!( + "try_normalize_erasing_regions::<{}>(value={:?}, param_env={:?})", + std::any::type_name::(), + value, + param_env, + ); + + // Erase first before we do the real query -- this keeps the + // cache from being too polluted. + let value = self.erase_regions(value); + debug!(?value); + + if !value.has_projections() { + Ok(value) + } else { + let mut folder = TryNormalizeAfterErasingRegionsFolder::new(self, param_env); + let result = value.fold_with(&mut folder); + + match folder.found_normalization_error() { + Some(e) => Err(e), + None => Ok(result), + } + } + } + /// If you have a `Binder<'tcx, T>`, you can do this to strip out the /// late-bound regions and then normalize the result, yielding up /// a `T` (with regions erased). This is appropriate when the @@ -91,11 +149,14 @@ struct NormalizeAfterErasingRegionsFolder<'tcx> { } impl<'tcx> NormalizeAfterErasingRegionsFolder<'tcx> { + #[instrument(skip(self), level = "debug")] fn normalize_generic_arg_after_erasing_regions( &self, arg: ty::GenericArg<'tcx>, ) -> ty::GenericArg<'tcx> { let arg = self.param_env.and(arg); + debug!(?arg); + self.tcx.normalize_generic_arg_after_erasing_regions(arg) } } @@ -126,3 +187,69 @@ impl TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> { Ok(self.tcx.normalize_mir_const_after_erasing_regions(arg)) } } + +struct TryNormalizeAfterErasingRegionsFolder<'tcx> { + tcx: TyCtxt<'tcx>, + param_env: ty::ParamEnv<'tcx>, + normalization_error: Option>, +} + +impl<'tcx> TryNormalizeAfterErasingRegionsFolder<'tcx> { + fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self { + TryNormalizeAfterErasingRegionsFolder { tcx, param_env, normalization_error: None } + } + + #[instrument(skip(self), level = "debug")] + fn try_normalize_generic_arg_after_erasing_regions( + &self, + arg: ty::GenericArg<'tcx>, + ) -> Result, NoSolution> { + let arg = self.param_env.and(arg); + debug!(?arg); + + self.tcx.try_normalize_generic_arg_after_erasing_regions(arg) + } + + pub fn found_normalization_error(&self) -> Option> { + self.normalization_error + } +} + +impl TypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'tcx> { + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } + + fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { + match self.try_normalize_generic_arg_after_erasing_regions(ty.into()) { + Ok(t) => t.expect_ty(), + Err(_) => { + self.normalization_error = Some(NormalizationError::Type(ty)); + ty + } + } + } + + fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + match self.try_normalize_generic_arg_after_erasing_regions(c.into()) { + Ok(t) => t.expect_const(), + Err(_) => { + self.normalization_error = Some(NormalizationError::Const(*c)); + c + } + } + } + + #[inline] + fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { + // FIXME: This *probably* needs canonicalization too! + let arg = self.param_env.and(c); + match self.tcx.try_normalize_mir_const_after_erasing_regions(arg) { + Ok(c) => c, + Err(_) => { + self.normalization_error = Some(NormalizationError::ConstantKind(c)); + c + } + } + } +} diff --git a/compiler/rustc_traits/src/normalize_erasing_regions.rs b/compiler/rustc_traits/src/normalize_erasing_regions.rs index 61ab5e28b6796..4f35909df7f6a 100644 --- a/compiler/rustc_traits/src/normalize_erasing_regions.rs +++ b/compiler/rustc_traits/src/normalize_erasing_regions.rs @@ -20,6 +20,14 @@ crate fn provide(p: &mut Providers) { normalize_mir_const_after_erasing_regions: |tcx, goal| { normalize_after_erasing_regions(tcx, goal) }, + try_normalize_generic_arg_after_erasing_regions: |tcx, goal| { + debug!("try_normalize_generic_arg_after_erasing_regions(goal={:#?}", goal); + + try_normalize_after_erasing_regions(tcx, goal) + }, + try_normalize_mir_const_after_erasing_regions: |tcx, goal| { + try_normalize_after_erasing_regions(tcx, goal) + }, ..*p }; } @@ -56,6 +64,38 @@ fn normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Cop }) } +#[instrument(level = "debug", skip(tcx))] +fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Copy>( + tcx: TyCtxt<'tcx>, + goal: ParamEnvAnd<'tcx, T>, +) -> Result { + let ParamEnvAnd { param_env, value } = goal; + tcx.infer_ctxt().enter(|infcx| { + let cause = ObligationCause::dummy(); + match infcx.at(&cause, param_env).normalize(value) { + Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => { + // We don't care about the `obligations`; they are + // always only region relations, and we are about to + // erase those anyway: + debug_assert_eq!( + normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)), + None, + ); + + let resolved_value = infcx.resolve_vars_if_possible(normalized_value); + // It's unclear when `resolve_vars` would have an effect in a + // fresh `InferCtxt`. If this assert does trigger, it will give + // us a test case. + debug_assert_eq!(normalized_value, resolved_value); + let erased = infcx.tcx.erase_regions(resolved_value); + debug_assert!(!erased.needs_infer(), "{:?}", erased); + Ok(erased) + } + Err(NoSolution) => Err(NoSolution), + } + }) +} + fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool { match p.kind().skip_binder() { ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false, From e0c98e2a33756507357534c24d5071ad93b23024 Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 26 Nov 2021 18:37:29 +0100 Subject: [PATCH 2/7] add tests and bless existing ones --- src/test/ui/associated-types/issue-59324.rs | 26 ++++ .../ui/associated-types/issue-59324.stderr | 69 ++++++++++ src/test/ui/associated-types/issue-67684.rs | 62 +++++++++ src/test/ui/associated-types/issue-69398.rs | 21 +++ src/test/ui/associated-types/issue-71113.rs | 16 +++ src/test/ui/associated-types/issue-82079.rs | 121 ++++++++++++++++++ src/test/ui/associated-types/issue-88856.rs | 32 +++++ src/test/ui/associated-types/issue-91234.rs | 13 ++ 8 files changed, 360 insertions(+) create mode 100644 src/test/ui/associated-types/issue-59324.rs create mode 100644 src/test/ui/associated-types/issue-59324.stderr create mode 100644 src/test/ui/associated-types/issue-67684.rs create mode 100644 src/test/ui/associated-types/issue-69398.rs create mode 100644 src/test/ui/associated-types/issue-71113.rs create mode 100644 src/test/ui/associated-types/issue-82079.rs create mode 100644 src/test/ui/associated-types/issue-88856.rs create mode 100644 src/test/ui/associated-types/issue-91234.rs diff --git a/src/test/ui/associated-types/issue-59324.rs b/src/test/ui/associated-types/issue-59324.rs new file mode 100644 index 0000000000000..9e68e9e77515b --- /dev/null +++ b/src/test/ui/associated-types/issue-59324.rs @@ -0,0 +1,26 @@ +trait NotFoo {} + +pub trait Foo: NotFoo { + type OnlyFoo; +} + +pub trait Service { + type AssocType; +} + +pub trait ThriftService: +//~^ ERROR the trait bound `Bug: Foo` is not satisfied +//~| ERROR the trait bound `Bug: Foo` is not satisfied + Service::OnlyFoo> +{ + fn get_service( + //~^ ERROR the trait bound `Bug: Foo` is not satisfied + //~| ERROR the trait bound `Bug: Foo` is not satisfied + &self, + ) -> Self::AssocType; +} + +fn with_factory(factory: dyn ThriftService<()>) {} +//~^ ERROR the trait bound `(): Foo` is not satisfied + +fn main() {} diff --git a/src/test/ui/associated-types/issue-59324.stderr b/src/test/ui/associated-types/issue-59324.stderr new file mode 100644 index 0000000000000..2f430d3055e19 --- /dev/null +++ b/src/test/ui/associated-types/issue-59324.stderr @@ -0,0 +1,69 @@ +error[E0277]: the trait bound `Bug: Foo` is not satisfied + --> $DIR/issue-59324.rs:11:1 + | +LL | / pub trait ThriftService: +LL | | +LL | | +LL | | Service::OnlyFoo> +... | +LL | | ) -> Self::AssocType; +LL | | } + | |_^ the trait `Foo` is not implemented for `Bug` + | +help: consider further restricting this bound + | +LL | pub trait ThriftService: + | +++++ + +error[E0277]: the trait bound `Bug: Foo` is not satisfied + --> $DIR/issue-59324.rs:11:1 + | +LL | / pub trait ThriftService: +LL | | +LL | | +LL | | Service::OnlyFoo> +... | +LL | | ) -> Self::AssocType; +LL | | } + | |_^ the trait `Foo` is not implemented for `Bug` + | +help: consider further restricting this bound + | +LL | pub trait ThriftService: + | +++++ + +error[E0277]: the trait bound `Bug: Foo` is not satisfied + --> $DIR/issue-59324.rs:16:5 + | +LL | / fn get_service( +LL | | +LL | | +LL | | &self, +LL | | ) -> Self::AssocType; + | |_________________________^ the trait `Foo` is not implemented for `Bug` + | +help: consider further restricting this bound + | +LL | pub trait ThriftService: + | +++++ + +error[E0277]: the trait bound `Bug: Foo` is not satisfied + --> $DIR/issue-59324.rs:16:8 + | +LL | fn get_service( + | ^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug` + | +help: consider further restricting this bound + | +LL | pub trait ThriftService: + | +++++ + +error[E0277]: the trait bound `(): Foo` is not satisfied + --> $DIR/issue-59324.rs:23:29 + | +LL | fn with_factory(factory: dyn ThriftService<()>) {} + | ^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `()` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/associated-types/issue-67684.rs b/src/test/ui/associated-types/issue-67684.rs new file mode 100644 index 0000000000000..49efe8a1bdaac --- /dev/null +++ b/src/test/ui/associated-types/issue-67684.rs @@ -0,0 +1,62 @@ +// check-pass + +#![allow(dead_code)] + +trait ParseError { + type StreamError; +} + +impl ParseError for T { + type StreamError = (); +} + +trait Stream { + type Item; + type Error: ParseError; +} + +trait Parser +where + ::PartialState: Default, +{ + type PartialState; + fn parse_mode(_: &Self, _: Self::PartialState) { + loop {} + } +} + +impl Stream for () { + type Item = (); + type Error = (); +} + +impl Parser for () { + type PartialState = (); +} + +struct AndThen(core::marker::PhantomData<(A, B)>); + +impl Parser for AndThen +where + A: Stream, + B: Into<::StreamError>, +{ + type PartialState = (); +} + +fn expr() -> impl Parser +where + A: Stream::Item>, +{ + AndThen::(core::marker::PhantomData) +} + +fn parse_mode_impl() +where + ::Error: ParseError, + A: Stream::Item>, +{ + Parser::parse_mode(&expr::(), Default::default()) +} + +fn main() {} diff --git a/src/test/ui/associated-types/issue-69398.rs b/src/test/ui/associated-types/issue-69398.rs new file mode 100644 index 0000000000000..ca3d66b1c8eb7 --- /dev/null +++ b/src/test/ui/associated-types/issue-69398.rs @@ -0,0 +1,21 @@ +// check-pass + +pub trait Foo { + type Bar; +} + +pub trait Broken { + type Assoc; + fn broken(&self) where Self::Assoc: Foo; +} + +impl Broken for T { + type Assoc = (); + fn broken(&self) where Self::Assoc: Foo { + let _x: ::Bar; + } +} + +fn main() { + let _m: &dyn Broken = &(); +} diff --git a/src/test/ui/associated-types/issue-71113.rs b/src/test/ui/associated-types/issue-71113.rs new file mode 100644 index 0000000000000..48de89127f4a5 --- /dev/null +++ b/src/test/ui/associated-types/issue-71113.rs @@ -0,0 +1,16 @@ +// check-pass + +use std::borrow::Cow; + +enum _Recursive<'a> +where + Self: ToOwned> +{ + Variant(MyCow<'a, _Recursive<'a>>), +} + +pub struct Wrapper(T); + +pub struct MyCow<'a, T: ToOwned> + 'a>(Wrapper>); + +fn main() {} diff --git a/src/test/ui/associated-types/issue-82079.rs b/src/test/ui/associated-types/issue-82079.rs new file mode 100644 index 0000000000000..590c799c2d71c --- /dev/null +++ b/src/test/ui/associated-types/issue-82079.rs @@ -0,0 +1,121 @@ +// check-pass + +mod convenience_operators { + use crate::{Op, Relation}; + use std::ops::AddAssign; + use std::ops::Mul; + + impl Relation { + pub fn map D2 + 'static, D2: 'static>( + self, + f: F, + ) -> Relation> { + self.map_dr(move |x, r| (f(x), r)) + } + } + + impl> Relation { + pub fn semijoin, R2, R3: AddAssign>( + self, + other: Relation, + ) -> Relation> + where + C::R: Mul, + { + self.join(other.map(|x| (x, ()))).map(|(k, x, ())| (k, x)) + } + } +} + +mod core { + mod operator { + mod join { + use super::Op; + use crate::core::Relation; + use std::ops::{AddAssign, Mul}; + struct Join { + _left: LC, + _right: RC, + } + impl< + LC: Op, + RC: Op, + K: 'static, + LD: 'static, + LR: AddAssign + Mul, + RD: 'static, + RR: AddAssign, + OR: AddAssign, + > Op for Join + { + type D = (K, LD, RD); + type R = OR; + } + impl> Relation { + pub fn join, D2: 'static, OR: AddAssign>( + self, + other: Relation, + ) -> Relation> + where + C::R: Mul, + { + Relation { + inner: Join { + _left: self.inner, + _right: other.inner, + }, + } + } + } + } + mod map { + use super::Op; + use crate::core::Relation; + use std::ops::AddAssign; + struct Map { + _inner: C, + _op: MF, + } + impl< + D1, + R1, + D2: 'static, + R2: AddAssign, + C: Op, + MF: Fn(D1, R1) -> (D2, R2), + > Op for Map + { + type D = D2; + type R = R2; + } + impl Relation { + pub fn map_dr (D2, R2), D2: 'static, R2: AddAssign>( + self, + f: F, + ) -> Relation> { + Relation { + inner: Map { + _inner: self.inner, + _op: f, + }, + } + } + } + } + use std::ops::AddAssign; + pub trait Op { + type D: 'static; + type R: AddAssign; + } + } + pub use self::operator::Op; + #[derive(Clone)] + pub struct Relation { + inner: C, + } +} + +use self::core::Op; +pub use self::core::Relation; + +fn main() {} diff --git a/src/test/ui/associated-types/issue-88856.rs b/src/test/ui/associated-types/issue-88856.rs new file mode 100644 index 0000000000000..7cae7c71cd2d0 --- /dev/null +++ b/src/test/ui/associated-types/issue-88856.rs @@ -0,0 +1,32 @@ +// check-pass + +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] + +pub trait Trait{ + type R; + fn func(self)->Self::R; +} + +pub struct TraitImpl(pub i32); + +impl Trait for TraitImpl +where [();N/2]:, +{ + type R = Self; + fn func(self)->Self::R { + self + } +} + +fn sample(p:P,f:Convert) -> i32 +where + P:Trait,Convert:Fn(P::R)->i32 +{ + f(p.func()) +} + +fn main() { + let t = TraitImpl::<10>(4); + sample(t,|x|x.0); +} diff --git a/src/test/ui/associated-types/issue-91234.rs b/src/test/ui/associated-types/issue-91234.rs new file mode 100644 index 0000000000000..2f6c2d3aebd0a --- /dev/null +++ b/src/test/ui/associated-types/issue-91234.rs @@ -0,0 +1,13 @@ +// check-pass + +struct Struct; + +trait Trait { + type Type; +} + +enum Enum<'a> where &'a Struct: Trait { + Variant(<&'a Struct as Trait>::Type) +} + +fn main() {} From a040b4189d326b25559041bd2c6452bfe0c9cfa7 Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 26 Nov 2021 23:37:24 +0100 Subject: [PATCH 3/7] more fixed issues --- src/librustdoc/html/render/print_item.rs | 7 +++++++ src/test/ui/associated-types/issue-85103.rs | 9 +++++++++ src/test/ui/associated-types/issue-85103.stderr | 8 ++++++++ src/test/ui/associated-types/issue-91231.rs | 17 +++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 src/test/ui/associated-types/issue-85103.rs create mode 100644 src/test/ui/associated-types/issue-85103.stderr create mode 100644 src/test/ui/associated-types/issue-91231.rs diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index d3738cfa3e781..48dd69155cea9 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1769,6 +1769,13 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) { the type was too big.

" ); } + Err(LayoutError::NormalizationFailure(_, _)) => { + writeln!( + w, + "

Note: Encountered an error during type layout; \ + the type was not normalizable.

" + ) + } } writeln!(w, ""); diff --git a/src/test/ui/associated-types/issue-85103.rs b/src/test/ui/associated-types/issue-85103.rs new file mode 100644 index 0000000000000..c5e13856178de --- /dev/null +++ b/src/test/ui/associated-types/issue-85103.rs @@ -0,0 +1,9 @@ +#![feature(rustc_attrs)] + +use std::borrow::Cow; + +#[rustc_layout(debug)] +type Edges<'a, E> = Cow<'a, [E]>; +//~^ ERROR layout error: NormalizationFailure + +fn main() {} diff --git a/src/test/ui/associated-types/issue-85103.stderr b/src/test/ui/associated-types/issue-85103.stderr new file mode 100644 index 0000000000000..142f3c411ec5c --- /dev/null +++ b/src/test/ui/associated-types/issue-85103.stderr @@ -0,0 +1,8 @@ +error: layout error: NormalizationFailure(<[E] as std::borrow::ToOwned>::Owned, Type(<[E] as std::borrow::ToOwned>::Owned)) + --> $DIR/issue-85103.rs:6:1 + | +LL | type Edges<'a, E> = Cow<'a, [E]>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/associated-types/issue-91231.rs b/src/test/ui/associated-types/issue-91231.rs new file mode 100644 index 0000000000000..3c1cb81f09756 --- /dev/null +++ b/src/test/ui/associated-types/issue-91231.rs @@ -0,0 +1,17 @@ +// check-pass + +#![feature(extern_types)] +#![allow(dead_code)] + +extern { + type Extern; +} + +trait Trait { + type Type; +} + +#[inline] +fn f<'a>(_: <&'a Extern as Trait>::Type) where &'a Extern: Trait {} + +fn main() {} From 0b32cf3a8d5bda2fb2599e658102e2d7bd70a07f Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 26 Nov 2021 23:39:48 +0100 Subject: [PATCH 4/7] remove static_assert_size on InterpError --- compiler/rustc_middle/src/mir/interpret/error.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 3e307979f2d0a..8e4a17bfa65cb 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -492,9 +492,6 @@ impl dyn MachineStopType { } } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(InterpError<'_>, 88); - pub enum InterpError<'tcx> { /// The program caused undefined behavior. UndefinedBehavior(UndefinedBehaviorInfo<'tcx>), From 84bcd40927d0547d36df8f6419b4d060c8c5a840 Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 26 Nov 2021 23:41:22 +0100 Subject: [PATCH 5/7] fix query description --- compiler/rustc_middle/src/query/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 12665be335492..8c1ffa1be3af5 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1662,14 +1662,14 @@ rustc_queries! { query try_normalize_generic_arg_after_erasing_regions( goal: ParamEnvAnd<'tcx, GenericArg<'tcx>> ) -> Result, NoSolution> { - desc { "trying to normalize `{}`", goal.value } + desc { "normalizing `{}`", goal.value } } /// Do not call this query directly: invoke `try_normalize_erasing_regions` instead. query try_normalize_mir_const_after_erasing_regions( goal: ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>> ) -> Result, NoSolution> { - desc { "trying to normalize `{}`", goal.value } + desc { "normalizing `{}`", goal.value } } query implied_outlives_bounds( From 4d9a0bf21b5429c9e3d08b4a735c40d60114ba5f Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 1 Dec 2021 00:20:57 +0100 Subject: [PATCH 6/7] address review --- compiler/rustc_middle/src/query/mod.rs | 5 +++++ compiler/rustc_middle/src/ty/layout.rs | 4 ++++ src/librustdoc/html/render/print_item.rs | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 8c1ffa1be3af5..8667a6bea11f6 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1644,6 +1644,11 @@ rustc_queries! { desc { "normalizing `{:?}`", goal } } + // FIXME: Implement `normalize_generic_arg_after_erasing_regions` and + // `normalize_mir_const_after_erasing_regions` in terms of + // `try_normalize_generic_arg_after_erasing_regions` and + // `try_normalize_mir_const_after_erasing_regions`, respectively. + /// Do not call this query directly: invoke `normalize_erasing_regions` instead. query normalize_generic_arg_after_erasing_regions( goal: ParamEnvAnd<'tcx, GenericArg<'tcx>> diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 9c1407de812f5..57506bc68345b 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -240,6 +240,10 @@ fn layout_of<'tcx>( let param_env = param_env.with_reveal_all_normalized(tcx); let unnormalized_ty = ty; + // FIXME: We might want to have two different versions of `layout_of`: + // One that can be called after typecheck has completed and can use + // `normalize_erasing_regions` here and another one that can be called + // before typecheck has completed and uses `try_normalize_erasing_regions`. let ty = match tcx.try_normalize_erasing_regions(param_env, ty) { Ok(t) => t, Err(normalization_error) => { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 48dd69155cea9..62fdec15af420 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1773,7 +1773,7 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) { writeln!( w, "

Note: Encountered an error during type layout; \ - the type was not normalizable.

" + the type failed to be normalized.

" ) } } From 6952470095180e74d59ae372d06a75818368000b Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 1 Dec 2021 13:14:19 +0100 Subject: [PATCH 7/7] rebase --- .../src/ty/normalize_erasing_regions.rs | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs index 0915228d07022..fce7cbfbb3d1e 100644 --- a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs +++ b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs @@ -90,12 +90,7 @@ impl<'tcx> TyCtxt<'tcx> { Ok(value) } else { let mut folder = TryNormalizeAfterErasingRegionsFolder::new(self, param_env); - let result = value.fold_with(&mut folder); - - match folder.found_normalization_error() { - Some(e) => Err(e), - None => Ok(result), - } + value.fold_with(&mut folder) } } @@ -191,12 +186,11 @@ impl TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> { struct TryNormalizeAfterErasingRegionsFolder<'tcx> { tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, - normalization_error: Option>, } impl<'tcx> TryNormalizeAfterErasingRegionsFolder<'tcx> { fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self { - TryNormalizeAfterErasingRegionsFolder { tcx, param_env, normalization_error: None } + TryNormalizeAfterErasingRegionsFolder { tcx, param_env } } #[instrument(skip(self), level = "debug")] @@ -209,47 +203,41 @@ impl<'tcx> TryNormalizeAfterErasingRegionsFolder<'tcx> { self.tcx.try_normalize_generic_arg_after_erasing_regions(arg) } - - pub fn found_normalization_error(&self) -> Option> { - self.normalization_error - } } impl TypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'tcx> { + type Error = NormalizationError<'tcx>; + fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } - fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { + fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result, Self::Error> { match self.try_normalize_generic_arg_after_erasing_regions(ty.into()) { - Ok(t) => t.expect_ty(), - Err(_) => { - self.normalization_error = Some(NormalizationError::Type(ty)); - ty - } + Ok(t) => Ok(t.expect_ty()), + Err(_) => Err(NormalizationError::Type(ty)), } } - fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + fn fold_const( + &mut self, + c: &'tcx ty::Const<'tcx>, + ) -> Result<&'tcx ty::Const<'tcx>, Self::Error> { match self.try_normalize_generic_arg_after_erasing_regions(c.into()) { - Ok(t) => t.expect_const(), - Err(_) => { - self.normalization_error = Some(NormalizationError::Const(*c)); - c - } + Ok(t) => Ok(t.expect_const()), + Err(_) => Err(NormalizationError::Const(*c)), } } - #[inline] - fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { + fn fold_mir_const( + &mut self, + c: mir::ConstantKind<'tcx>, + ) -> Result, Self::Error> { // FIXME: This *probably* needs canonicalization too! let arg = self.param_env.and(c); match self.tcx.try_normalize_mir_const_after_erasing_regions(arg) { - Ok(c) => c, - Err(_) => { - self.normalization_error = Some(NormalizationError::ConstantKind(c)); - c - } + Ok(c) => Ok(c), + Err(_) => Err(NormalizationError::ConstantKind(c)), } } }