review, improve note span

This commit is contained in:
Bastian Kauschke 2020-09-08 11:37:27 +02:00
parent e5b82a56c5
commit c552717e9d
5 changed files with 40 additions and 41 deletions

View File

@ -201,11 +201,13 @@ pub enum Res<Id = hir::HirId> {
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::<Self>()]`.
/// 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::<Self>()] {} }
/// ```
///
/// 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<DefId> /* trait */, Option<(DefId, bool)> /* impl */),
ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`

View File

@ -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;
}
}

View File

@ -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

View File

@ -13,12 +13,10 @@ LL | fn t3() -> [u8; std::mem::size_of::<Self>()] {}
| ^^^^
|
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<T> Bar<T> {
LL | | fn t3() -> [u8; std::mem::size_of::<Self>()] {}
LL | | }
| |_^
LL | impl<T> Bar<T> {
| ^^^^^^
error: aborting due to 2 previous errors

View File

@ -5,14 +5,10 @@ LL | let _: [u8; std::mem::size_of::<Self>()];
| ^^^^
|
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<T> Baz for Bar<T> {
LL | | fn hey() {
LL | | let _: [u8; std::mem::size_of::<Self>()];
LL | | }
LL | | }
| |_^
LL | impl<T> Baz for Bar<T> {
| ^^^^^^
error: aborting due to previous error