Skip to content

Commit 4b7db5a

Browse files
committed
Reject escaping generic params in the type of assoc const bindings
1 parent 8bb49e2 commit 4b7db5a

File tree

5 files changed

+273
-4
lines changed

5 files changed

+273
-4
lines changed

compiler/rustc_hir_analysis/messages.ftl

+18
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@ hir_analysis_only_current_traits_primitive = only traits defined in the current
314314
315315
hir_analysis_only_current_traits_ty = `{$ty}` is not defined in the current crate
316316
317+
hir_analysis_param_in_ty_of_assoc_const_binding =
318+
the type of the associated constant `{$assoc_const}` must not depend on {$param_category ->
319+
[self] `Self`
320+
[synthetic] `impl Trait`
321+
*[normal] generic parameters
322+
}
323+
.label = its type must not depend on {$param_category ->
324+
[self] `Self`
325+
[synthetic] `impl Trait`
326+
*[normal] the {$param_def_kind} `{$param_name}`
327+
}
328+
.param_defined_here_label = {$param_category ->
329+
[synthetic] the `impl Trait` is specified here
330+
*[normal] the {$param_def_kind} `{$param_name}` is defined here
331+
}
332+
317333
hir_analysis_paren_sugar_attribute = the `#[rustc_paren_sugar]` attribute is a temporary means of controlling which traits can use parenthetical notation
318334
.help = add `#![feature(unboxed_closures)]` to the crate attributes to use it
319335
@@ -424,6 +440,8 @@ hir_analysis_transparent_non_zero_sized_enum = the variant of a transparent {$de
424440
.label = needs at most one field with non-trivial size or alignment, but has {$field_count}
425441
.labels = this field has non-zero size or requires alignment
426442
443+
hir_analysis_ty_of_assoc_const_binding_note = `{$assoc_const}` has type `{$ty}`
444+
427445
hir_analysis_ty_param_first_local = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
428446
.label = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
429447
.note = implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type

compiler/rustc_hir_analysis/src/collect/type_of.rs

+101-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::ops::ControlFlow;
2+
3+
use rustc_data_structures::fx::FxIndexSet;
14
use rustc_errors::{Applicability, StashKey};
25
use rustc_hir as hir;
36
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -7,7 +10,8 @@ use rustc_middle::ty::print::with_forced_trimmed_paths;
710
use rustc_middle::ty::util::IntTypeExt;
811
use rustc_middle::ty::{self, ImplTraitInTraitData, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
912
use rustc_span::symbol::Ident;
10-
use rustc_span::{Span, DUMMY_SP};
13+
use rustc_span::{ErrorGuaranteed, Span, Symbol, DUMMY_SP};
14+
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
1115

1216
use crate::errors::TypeofReservedKeywordUsed;
1317

@@ -78,10 +82,23 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
7882
.expect("const parameter types cannot be generic");
7983
}
8084

81-
Node::TypeBinding(&TypeBinding { hir_id, .. }) => {
82-
// FIXME(fmease): Reject “escaping” early-bound generic parameters.
85+
Node::TypeBinding(&TypeBinding { hir_id, ident, .. }) => {
86+
let ty = tcx.type_of_assoc_const_binding(hir_id);
87+
88+
// We can't possibly catch this in the resolver, therefore we need to handle it here.
89+
// FIXME(const_generics): Support generic const generics.
90+
let Some(ty) = ty.no_bound_vars() else {
91+
let reported = report_overly_generic_assoc_const_binding_type(
92+
tcx,
93+
ident,
94+
ty.skip_binder().skip_binder(),
95+
hir_id,
96+
);
97+
return Ty::new_error(tcx, reported);
98+
};
99+
83100
// FIXME(fmease): Reject escaping late-bound vars.
84-
return tcx.type_of_assoc_const_binding(hir_id).skip_binder().skip_binder();
101+
return ty.skip_binder();
85102
}
86103

87104
// This match arm is for when the def_id appears in a GAT whose
@@ -302,6 +319,86 @@ pub(super) fn type_of_assoc_const_binding<'tcx>(
302319
ty::EarlyBinder::bind(ty::Binder::dummy(Ty::new_error(tcx, reported)))
303320
}
304321

322+
fn report_overly_generic_assoc_const_binding_type<'tcx>(
323+
tcx: TyCtxt<'tcx>,
324+
assoc_const: Ident,
325+
ty: Ty<'tcx>,
326+
hir_id: HirId,
327+
) -> ErrorGuaranteed {
328+
let mut collector = GenericParamCollector { params: Default::default() };
329+
ty.visit_with(&mut collector);
330+
331+
let mut reported = None;
332+
333+
let ty_note = ty
334+
.make_suggestable(tcx, false)
335+
.map(|ty| crate::errors::TyOfAssocConstBindingNote { assoc_const, ty });
336+
337+
// FIXME(fmease): Consider delaying a bug instead of unwrapping since maybe this could trigger
338+
// if we encountered some malformed code, dunno?
339+
let enclosing_item_owner_id = tcx
340+
.hir()
341+
.parent_owner_iter(hir_id)
342+
.find_map(|(owner_id, parent)| parent.generics().map(|_| owner_id))
343+
.unwrap();
344+
let generics = tcx.generics_of(enclosing_item_owner_id);
345+
for (param_index, param_name) in collector.params {
346+
let param_def = generics.param_at(param_index as _, tcx);
347+
let is_self_param = param_def.name == rustc_span::symbol::kw::SelfUpper;
348+
reported.get_or_insert(tcx.dcx().emit_err(crate::errors::ParamInTyOfAssocConstBinding {
349+
span: assoc_const.span,
350+
assoc_const,
351+
param_name,
352+
param_def_kind: tcx.def_descr(param_def.def_id),
353+
// FIXME(fmease): Yeah, I don't like this stringly-typed stuff either.
354+
param_category: if is_self_param {
355+
"self"
356+
} else if param_def.kind.is_synthetic() {
357+
"synthetic"
358+
} else {
359+
"normal"
360+
},
361+
param_defined_here_label:
362+
(!is_self_param).then(|| tcx.def_ident_span(param_def.def_id).unwrap()),
363+
ty_note,
364+
}));
365+
}
366+
367+
struct GenericParamCollector {
368+
params: FxIndexSet<(u32, Symbol)>,
369+
}
370+
371+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamCollector {
372+
type BreakTy = !;
373+
374+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
375+
if let ty::Param(param) = ty.kind() {
376+
self.params.insert((param.index, param.name));
377+
return ControlFlow::Continue(());
378+
}
379+
ty.super_visit_with(self)
380+
}
381+
382+
fn visit_region(&mut self, re: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
383+
if let ty::ReEarlyParam(param) = re.kind() {
384+
self.params.insert((param.index, param.name));
385+
return ControlFlow::Continue(());
386+
}
387+
ControlFlow::Continue(())
388+
}
389+
390+
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
391+
if let ty::ConstKind::Param(param) = ct.kind() {
392+
self.params.insert((param.index, param.name));
393+
return ControlFlow::Continue(());
394+
}
395+
ct.super_visit_with(self)
396+
}
397+
}
398+
399+
reported.unwrap_or_else(|| bug!("failed to find gen params in ty"))
400+
}
401+
305402
fn get_path_containing_arg_in_pat<'hir>(
306403
pat: &'hir hir::Pat<'hir>,
307404
arg_id: HirId,

compiler/rustc_hir_analysis/src/errors.rs

+23
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,29 @@ pub struct AssocTypeBindingNotAllowed {
295295
pub fn_trait_expansion: Option<ParenthesizedFnTraitExpansion>,
296296
}
297297

298+
#[derive(Diagnostic)]
299+
#[diag(hir_analysis_param_in_ty_of_assoc_const_binding)]
300+
pub(crate) struct ParamInTyOfAssocConstBinding<'tcx> {
301+
#[primary_span]
302+
#[label]
303+
pub span: Span,
304+
pub assoc_const: Ident,
305+
pub param_name: Symbol,
306+
pub param_def_kind: &'static str,
307+
pub param_category: &'static str,
308+
#[label(hir_analysis_param_defined_here_label)]
309+
pub param_defined_here_label: Option<Span>,
310+
#[subdiagnostic]
311+
pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
312+
}
313+
314+
#[derive(Subdiagnostic, Clone, Copy)]
315+
#[note(hir_analysis_ty_of_assoc_const_binding_note)]
316+
pub(crate) struct TyOfAssocConstBindingNote<'tcx> {
317+
pub assoc_const: Ident,
318+
pub ty: Ty<'tcx>,
319+
}
320+
298321
#[derive(Subdiagnostic)]
299322
#[help(hir_analysis_parenthesized_fn_trait_expansion)]
300323
pub struct ParenthesizedFnTraitExpansion {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Regression test for issue #108271.
2+
// Detect and reject generic params in the type of assoc consts used in an equality bound.
3+
#![feature(associated_const_equality)]
4+
5+
trait Trait<'a, T: 'a, const N: usize> {
6+
const K: &'a [T; N];
7+
}
8+
9+
fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
10+
//~^ ERROR the type of the associated constant `K` must not depend on generic parameters
11+
//~| NOTE its type must not depend on the lifetime parameter `'r`
12+
//~| NOTE the lifetime parameter `'r` is defined here
13+
//~| NOTE `K` has type `&'r [A; Q]`
14+
//~| ERROR the type of the associated constant `K` must not depend on generic parameters
15+
//~| NOTE its type must not depend on the type parameter `A`
16+
//~| NOTE the type parameter `A` is defined here
17+
//~| NOTE `K` has type `&'r [A; Q]`
18+
//~| ERROR the type of the associated constant `K` must not depend on generic parameters
19+
//~| NOTE its type must not depend on the const parameter `Q`
20+
//~| NOTE the const parameter `Q` is defined here
21+
//~| NOTE `K` has type `&'r [A; Q]`
22+
23+
trait Project {
24+
const SELF: Self;
25+
}
26+
27+
fn take1(_: impl Project<SELF = {}>) {}
28+
//~^ ERROR the type of the associated constant `SELF` must not depend on `impl Trait`
29+
//~| NOTE its type must not depend on `impl Trait`
30+
//~| NOTE the `impl Trait` is specified here
31+
32+
fn take2<P: Project<SELF = {}>>(_: P) {}
33+
//~^ ERROR the type of the associated constant `SELF` must not depend on generic parameters
34+
//~| NOTE its type must not depend on the type parameter `P`
35+
//~| NOTE the type parameter `P` is defined here
36+
//~| NOTE `SELF` has type `P`
37+
38+
trait Iface<'r> {
39+
//~^ NOTE the lifetime parameter `'r` is defined here
40+
type Assoc<const Q: usize>: Trait<'r, Self, Q, K = { loop {} }>
41+
//~^ ERROR the type of the associated constant `K` must not depend on generic parameters
42+
//~| NOTE its type must not depend on the lifetime parameter `'r`
43+
//~| NOTE `K` has type `&'r [Self; Q]`
44+
//~| ERROR the type of the associated constant `K` must not depend on `Self`
45+
//~| NOTE its type must not depend on `Self`
46+
//~| NOTE `K` has type `&'r [Self; Q]`
47+
//~| ERROR the type of the associated constant `K` must not depend on generic parameters
48+
//~| NOTE its type must not depend on the const parameter `Q`
49+
//~| NOTE the const parameter `Q` is defined here
50+
//~| NOTE `K` has type `&'r [Self; Q]`
51+
where
52+
Self: Sized + 'r;
53+
}
54+
55+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
error: the type of the associated constant `K` must not depend on generic parameters
2+
--> $DIR/assoc-const-eq-param-in-ty.rs:9:61
3+
|
4+
LL | fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
5+
| -- the lifetime parameter `'r` is defined here ^ its type must not depend on the lifetime parameter `'r`
6+
|
7+
= note: `K` has type `&'r [A; Q]`
8+
9+
error: the type of the associated constant `K` must not depend on generic parameters
10+
--> $DIR/assoc-const-eq-param-in-ty.rs:9:61
11+
|
12+
LL | fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
13+
| - the type parameter `A` is defined here ^ its type must not depend on the type parameter `A`
14+
|
15+
= note: `K` has type `&'r [A; Q]`
16+
17+
error: the type of the associated constant `K` must not depend on generic parameters
18+
--> $DIR/assoc-const-eq-param-in-ty.rs:9:61
19+
|
20+
LL | fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
21+
| - ^ its type must not depend on the const parameter `Q`
22+
| |
23+
| the const parameter `Q` is defined here
24+
|
25+
= note: `K` has type `&'r [A; Q]`
26+
27+
error: the type of the associated constant `SELF` must not depend on `impl Trait`
28+
--> $DIR/assoc-const-eq-param-in-ty.rs:27:26
29+
|
30+
LL | fn take1(_: impl Project<SELF = {}>) {}
31+
| -------------^^^^------
32+
| | |
33+
| | its type must not depend on `impl Trait`
34+
| the `impl Trait` is specified here
35+
36+
error: the type of the associated constant `SELF` must not depend on generic parameters
37+
--> $DIR/assoc-const-eq-param-in-ty.rs:32:21
38+
|
39+
LL | fn take2<P: Project<SELF = {}>>(_: P) {}
40+
| - ^^^^ its type must not depend on the type parameter `P`
41+
| |
42+
| the type parameter `P` is defined here
43+
|
44+
= note: `SELF` has type `P`
45+
46+
error: the type of the associated constant `K` must not depend on generic parameters
47+
--> $DIR/assoc-const-eq-param-in-ty.rs:40:52
48+
|
49+
LL | trait Iface<'r> {
50+
| -- the lifetime parameter `'r` is defined here
51+
LL |
52+
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = { loop {} }>
53+
| ^ its type must not depend on the lifetime parameter `'r`
54+
|
55+
= note: `K` has type `&'r [Self; Q]`
56+
57+
error: the type of the associated constant `K` must not depend on `Self`
58+
--> $DIR/assoc-const-eq-param-in-ty.rs:40:52
59+
|
60+
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = { loop {} }>
61+
| ^ its type must not depend on `Self`
62+
|
63+
= note: `K` has type `&'r [Self; Q]`
64+
65+
error: the type of the associated constant `K` must not depend on generic parameters
66+
--> $DIR/assoc-const-eq-param-in-ty.rs:40:52
67+
|
68+
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = { loop {} }>
69+
| - ^ its type must not depend on the const parameter `Q`
70+
| |
71+
| the const parameter `Q` is defined here
72+
|
73+
= note: `K` has type `&'r [Self; Q]`
74+
75+
error: aborting due to 8 previous errors
76+

0 commit comments

Comments
 (0)