Skip to content

Commit c552717

Browse files
committed
review, improve note span
1 parent e5b82a5 commit c552717

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

compiler/rustc_hir/src/def.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,13 @@ pub enum Res<Id = hir::HirId> {
201201
PrimTy(hir::PrimTy),
202202
/// `Self`, with both an optional trait and impl `DefId`.
203203
///
204-
/// HACK: impl self types also have an optional requirement to not mention
205-
/// any generic parameters to allow the following with `min_const_generics`.
206-
/// `impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()]`.
204+
/// HACK(min_const_generics): impl self types also have an optional requirement to not mention
205+
/// any generic parameters to allow the following with `min_const_generics`:
206+
/// ```rust
207+
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] {} }
208+
/// ```
207209
///
208-
/// Once `lazy_normalization_consts` is stable, this bodge can be removed again.
210+
/// FIXME(lazy_normalization_consts): Remove this bodge once this feature is stable.
209211
SelfTy(Option<DefId> /* trait */, Option<(DefId, bool)> /* impl */),
210212
ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
211213

compiler/rustc_resolve/src/lib.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -2627,25 +2627,23 @@ impl<'a> Resolver<'a> {
26272627
continue;
26282628
}
26292629
ConstantItemRibKind(trivial) => {
2630-
if self.session.features_untracked().min_const_generics {
2631-
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
2632-
if !trivial {
2633-
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
2634-
// we can't easily tell if it's generic at this stage, so we instead remember
2635-
// this and then enforce the self type to be concrete later on.
2636-
if let Res::SelfTy(trait_def, Some((impl_def, _))) = res {
2637-
res = Res::SelfTy(trait_def, Some((impl_def, true)));
2638-
} else {
2639-
if record_used {
2640-
self.report_error(
2641-
span,
2642-
ResolutionError::ParamInNonTrivialAnonConst(
2643-
rib_ident.name,
2644-
),
2645-
);
2646-
}
2647-
return Res::Err;
2630+
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
2631+
if !trivial && self.session.features_untracked().min_const_generics {
2632+
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
2633+
// we can't easily tell if it's generic at this stage, so we instead remember
2634+
// this and then enforce the self type to be concrete later on.
2635+
if let Res::SelfTy(trait_def, Some((impl_def, _))) = res {
2636+
res = Res::SelfTy(trait_def, Some((impl_def, true)));
2637+
} else {
2638+
if record_used {
2639+
self.report_error(
2640+
span,
2641+
ResolutionError::ParamInNonTrivialAnonConst(
2642+
rib_ident.name,
2643+
),
2644+
);
26482645
}
2646+
return Res::Err;
26492647
}
26502648
}
26512649

compiler/rustc_typeck/src/astconv/mod.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1924,13 +1924,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19241924
// Try to evaluate any array length constants.
19251925
let normalized_ty = self.normalize_ty(span, tcx.at(span).type_of(def_id));
19261926
if forbid_generic && normalized_ty.needs_subst() {
1927-
tcx.sess
1928-
.struct_span_err(
1929-
path.span,
1930-
"generic `Self` types are currently not permitted in anonymous constants"
1931-
)
1932-
.span_note(tcx.def_span(def_id), "not a concrete type")
1933-
.emit();
1927+
let mut err = tcx.sess.struct_span_err(
1928+
path.span,
1929+
"generic `Self` types are currently not permitted in anonymous constants",
1930+
);
1931+
if let Some(hir::Node::Item(&hir::Item {
1932+
kind: hir::ItemKind::Impl { self_ty, .. },
1933+
..
1934+
})) = tcx.hir().get_if_local(def_id)
1935+
{
1936+
err.span_note(self_ty.span, "not a concrete type");
1937+
}
1938+
err.emit();
19341939
tcx.ty_error()
19351940
} else {
19361941
normalized_ty

src/test/ui/const-generics/min_const_generics/self-ty-in-const-1.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ LL | fn t3() -> [u8; std::mem::size_of::<Self>()] {}
1313
| ^^^^
1414
|
1515
note: not a concrete type
16-
--> $DIR/self-ty-in-const-1.rs:13:1
16+
--> $DIR/self-ty-in-const-1.rs:13:9
1717
|
18-
LL | / impl<T> Bar<T> {
19-
LL | | fn t3() -> [u8; std::mem::size_of::<Self>()] {}
20-
LL | | }
21-
| |_^
18+
LL | impl<T> Bar<T> {
19+
| ^^^^^^
2220

2321
error: aborting due to 2 previous errors
2422

src/test/ui/const-generics/min_const_generics/self-ty-in-const-2.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ LL | let _: [u8; std::mem::size_of::<Self>()];
55
| ^^^^
66
|
77
note: not a concrete type
8-
--> $DIR/self-ty-in-const-2.rs:15:1
8+
--> $DIR/self-ty-in-const-2.rs:15:17
99
|
10-
LL | / impl<T> Baz for Bar<T> {
11-
LL | | fn hey() {
12-
LL | | let _: [u8; std::mem::size_of::<Self>()];
13-
LL | | }
14-
LL | | }
15-
| |_^
10+
LL | impl<T> Baz for Bar<T> {
11+
| ^^^^^^
1612

1713
error: aborting due to previous error
1814

0 commit comments

Comments
 (0)