Skip to content

Commit 7faa879

Browse files
committed
Pattern types: Prohibit generic args on const params
1 parent 7c4ac06 commit 7faa879

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

compiler/rustc_hir_analysis/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
371371
.suggestion = cast the value to `{$cast_ty}`
372372
.help = cast the value to `{$cast_ty}`
373373
374-
hir_analysis_pattern_type_non_const_range = "range patterns must have constant range start and end"
375-
hir_analysis_pattern_type_wild_pat = "wildcard patterns are not permitted for pattern types"
376-
.label = "this type is the same as the inner type without a pattern"
374+
hir_analysis_pattern_type_non_const_range = range patterns must have constant range start and end
375+
hir_analysis_pattern_type_wild_pat = wildcard patterns are not permitted for pattern types
376+
.label = this type is the same as the inner type without a pattern
377377
hir_analysis_placeholder_not_allowed_item_signatures = the placeholder `_` is not allowed within types on item signatures for {$kind}
378378
.label = not allowed in type signatures
379379

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ pub enum GenericsArgsErrExtend<'tcx> {
13881388
span: Span,
13891389
},
13901390
SelfTyParam(Span),
1391-
TyParam(DefId),
1391+
Param(DefId),
13921392
DefVariant,
13931393
None,
13941394
}
@@ -1504,11 +1504,11 @@ fn generics_args_err_extend<'a>(
15041504
GenericsArgsErrExtend::DefVariant => {
15051505
err.note("enum variants can't have type parameters");
15061506
}
1507-
GenericsArgsErrExtend::TyParam(def_id) => {
1508-
if let Some(span) = tcx.def_ident_span(def_id) {
1509-
let name = tcx.item_name(def_id);
1510-
err.span_note(span, format!("type parameter `{name}` defined here"));
1511-
}
1507+
GenericsArgsErrExtend::Param(def_id) => {
1508+
let span = tcx.def_ident_span(def_id).unwrap();
1509+
let kind = tcx.def_descr(def_id);
1510+
let name = tcx.item_name(def_id);
1511+
err.span_note(span, format!("{kind} `{name}` defined here"));
15121512
}
15131513
GenericsArgsErrExtend::SelfTyParam(span) => {
15141514
err.span_suggestion_verbose(

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
17561756
assert_eq!(opt_self_ty, None);
17571757
let _ = self.prohibit_generic_args(
17581758
path.segments.iter(),
1759-
GenericsArgsErrExtend::TyParam(def_id),
1759+
GenericsArgsErrExtend::Param(def_id),
17601760
);
17611761
self.lower_ty_param(hir_id)
17621762
}
@@ -2196,10 +2196,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21962196

21972197
hir::ExprKind::Path(hir::QPath::Resolved(
21982198
_,
2199-
&hir::Path {
2200-
res: Res::Def(DefKind::ConstParam, def_id), ..
2199+
path @ &hir::Path {
2200+
res: Res::Def(DefKind::ConstParam, def_id),
2201+
..
22012202
},
22022203
)) => {
2204+
let _ = self.prohibit_generic_args(
2205+
path.segments.iter(),
2206+
GenericsArgsErrExtend::Param(def_id),
2207+
);
22032208
let ty = tcx
22042209
.type_of(def_id)
22052210
.no_bound_vars()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(pattern_types, core_pattern_type)]
2+
#![allow(internal_features)]
3+
4+
type Pat<const START: u32, const END: u32> =
5+
std::pat::pattern_type!(u32 is START::<(), i32, 2>..=END::<_, Assoc = ()>);
6+
//~^ ERROR type and const arguments are not allowed on const parameter `START`
7+
//~| ERROR type arguments are not allowed on const parameter `END`
8+
//~| ERROR associated type bindings are not allowed here
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0109]: type and const arguments are not allowed on const parameter `START`
2+
--> $DIR/bad_const_generics_args_on_const_param.rs:5:44
3+
|
4+
LL | std::pat::pattern_type!(u32 is START::<(), i32, 2>..=END::<_, Assoc = ()>);
5+
| ----- ^^ ^^^ ^ type and const arguments not allowed
6+
| |
7+
| not allowed on const parameter `START`
8+
|
9+
note: const parameter `START` defined here
10+
--> $DIR/bad_const_generics_args_on_const_param.rs:4:16
11+
|
12+
LL | type Pat<const START: u32, const END: u32> =
13+
| ^^^^^
14+
15+
error[E0109]: type arguments are not allowed on const parameter `END`
16+
--> $DIR/bad_const_generics_args_on_const_param.rs:5:64
17+
|
18+
LL | std::pat::pattern_type!(u32 is START::<(), i32, 2>..=END::<_, Assoc = ()>);
19+
| --- ^ type argument not allowed
20+
| |
21+
| not allowed on const parameter `END`
22+
|
23+
note: const parameter `END` defined here
24+
--> $DIR/bad_const_generics_args_on_const_param.rs:4:34
25+
|
26+
LL | type Pat<const START: u32, const END: u32> =
27+
| ^^^
28+
29+
error[E0229]: associated type bindings are not allowed here
30+
--> $DIR/bad_const_generics_args_on_const_param.rs:5:67
31+
|
32+
LL | std::pat::pattern_type!(u32 is START::<(), i32, 2>..=END::<_, Assoc = ()>);
33+
| ^^^^^^^^^^ associated type not allowed here
34+
35+
error: aborting due to 3 previous errors
36+
37+
Some errors have detailed explanations: E0109, E0229.
38+
For more information about an error, try `rustc --explain E0109`.

tests/ui/type/pattern_types/bad_pat.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | type Positive2 = pattern_type!(i32 is 0..=);
1414
|
1515
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
1616

17-
error: "wildcard patterns are not permitted for pattern types"
17+
error: wildcard patterns are not permitted for pattern types
1818
--> $DIR/bad_pat.rs:11:33
1919
|
2020
LL | type Wild = pattern_type!(() is _);

0 commit comments

Comments
 (0)