From c552717e9da86f70d49390bba1e4b305054d9ed4 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 8 Sep 2020 11:37:27 +0200 Subject: [PATCH] review, improve note span --- compiler/rustc_hir/src/def.rs | 10 +++--- compiler/rustc_resolve/src/lib.rs | 34 +++++++++---------- compiler/rustc_typeck/src/astconv/mod.rs | 19 +++++++---- .../self-ty-in-const-1.stderr | 8 ++--- .../self-ty-in-const-2.stderr | 10 ++---- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index 730059e7ece..96fde48d96c 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -201,11 +201,13 @@ pub enum Res { PrimTy(hir::PrimTy), /// `Self`, with both an optional trait and impl `DefId`. /// - /// HACK: impl self types also have an optional requirement to not mention - /// any generic parameters to allow the following with `min_const_generics`. - /// `impl Foo { fn test() -> [u8; std::mem::size_of::()]`. + /// HACK(min_const_generics): impl self types also have an optional requirement to not mention + /// any generic parameters to allow the following with `min_const_generics`: + /// ```rust + /// impl Foo { fn test() -> [u8; std::mem::size_of::()] {} } + /// ``` /// - /// Once `lazy_normalization_consts` is stable, this bodge can be removed again. + /// FIXME(lazy_normalization_consts): Remove this bodge once this feature is stable. SelfTy(Option /* trait */, Option<(DefId, bool)> /* impl */), ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]` diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 1922f0d566e..00a37d908cd 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -2627,25 +2627,23 @@ impl<'a> Resolver<'a> { continue; } ConstantItemRibKind(trivial) => { - if self.session.features_untracked().min_const_generics { - // HACK(min_const_generics): We currently only allow `N` or `{ N }`. - if !trivial { - // HACK(min_const_generics): If we encounter `Self` in an anonymous constant - // we can't easily tell if it's generic at this stage, so we instead remember - // this and then enforce the self type to be concrete later on. - if let Res::SelfTy(trait_def, Some((impl_def, _))) = res { - res = Res::SelfTy(trait_def, Some((impl_def, true))); - } else { - if record_used { - self.report_error( - span, - ResolutionError::ParamInNonTrivialAnonConst( - rib_ident.name, - ), - ); - } - return Res::Err; + // HACK(min_const_generics): We currently only allow `N` or `{ N }`. + if !trivial && self.session.features_untracked().min_const_generics { + // HACK(min_const_generics): If we encounter `Self` in an anonymous constant + // we can't easily tell if it's generic at this stage, so we instead remember + // this and then enforce the self type to be concrete later on. + if let Res::SelfTy(trait_def, Some((impl_def, _))) = res { + res = Res::SelfTy(trait_def, Some((impl_def, true))); + } else { + if record_used { + self.report_error( + span, + ResolutionError::ParamInNonTrivialAnonConst( + rib_ident.name, + ), + ); } + return Res::Err; } } diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 66d9d49d93f..a743dc1cd20 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -1924,13 +1924,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // Try to evaluate any array length constants. let normalized_ty = self.normalize_ty(span, tcx.at(span).type_of(def_id)); if forbid_generic && normalized_ty.needs_subst() { - tcx.sess - .struct_span_err( - path.span, - "generic `Self` types are currently not permitted in anonymous constants" - ) - .span_note(tcx.def_span(def_id), "not a concrete type") - .emit(); + let mut err = tcx.sess.struct_span_err( + path.span, + "generic `Self` types are currently not permitted in anonymous constants", + ); + if let Some(hir::Node::Item(&hir::Item { + kind: hir::ItemKind::Impl { self_ty, .. }, + .. + })) = tcx.hir().get_if_local(def_id) + { + err.span_note(self_ty.span, "not a concrete type"); + } + err.emit(); tcx.ty_error() } else { normalized_ty diff --git a/src/test/ui/const-generics/min_const_generics/self-ty-in-const-1.stderr b/src/test/ui/const-generics/min_const_generics/self-ty-in-const-1.stderr index 94f67735fca..89ce58564e4 100644 --- a/src/test/ui/const-generics/min_const_generics/self-ty-in-const-1.stderr +++ b/src/test/ui/const-generics/min_const_generics/self-ty-in-const-1.stderr @@ -13,12 +13,10 @@ LL | fn t3() -> [u8; std::mem::size_of::()] {} | ^^^^ | note: not a concrete type - --> $DIR/self-ty-in-const-1.rs:13:1 + --> $DIR/self-ty-in-const-1.rs:13:9 | -LL | / impl Bar { -LL | | fn t3() -> [u8; std::mem::size_of::()] {} -LL | | } - | |_^ +LL | impl Bar { + | ^^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/min_const_generics/self-ty-in-const-2.stderr b/src/test/ui/const-generics/min_const_generics/self-ty-in-const-2.stderr index 70f44e7de63..9ac6410a290 100644 --- a/src/test/ui/const-generics/min_const_generics/self-ty-in-const-2.stderr +++ b/src/test/ui/const-generics/min_const_generics/self-ty-in-const-2.stderr @@ -5,14 +5,10 @@ LL | let _: [u8; std::mem::size_of::()]; | ^^^^ | note: not a concrete type - --> $DIR/self-ty-in-const-2.rs:15:1 + --> $DIR/self-ty-in-const-2.rs:15:17 | -LL | / impl Baz for Bar { -LL | | fn hey() { -LL | | let _: [u8; std::mem::size_of::()]; -LL | | } -LL | | } - | |_^ +LL | impl Baz for Bar { + | ^^^^^^ error: aborting due to previous error