Skip to content

Commit a1fc6c6

Browse files
authored
Unrolled build for rust-lang#125015
Rollup merge of rust-lang#125015 - fmease:pat-tys-proh-gen-args-on-ct-params, r=spastorino Pattern types: Prohibit generic args on const params Addresses https://github.com/rust-lang/rust/pull/123689/files#r1562676629. NB: Technically speaking, *not* prohibiting generics args on const params is not a bug as `pattern_types` is an *internal* feature and as such any uncaught misuses of it are considered to be the fault of the user. However, permitting this makes me slightly uncomfortable esp. since we might want to make pattern types available to the public at some point and I don't want this oversight to be able to slip into the language (for comparison, ICEs triggered by the use of internal features are like super fine). Furthermore, this is an ad hoc fix. A more general fix would be changing the representation of the pattern part of pattern types in such a way that it can reuse preexisting lowering routines for exprs / anon consts. See also this [Zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/pattern.20type.20HIR.20nodes/near/432410768) and rust-lang#124650. Also note that we currently don't properly typeck the pattern of pat tys. This however is out of scope for this PR. cc ``@oli-obk`` r? ``@spastorino`` as discussed
2 parents 22f5bdc + 7faa879 commit a1fc6c6

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
@@ -1382,7 +1382,7 @@ pub enum GenericsArgsErrExtend<'tcx> {
13821382
span: Span,
13831383
},
13841384
SelfTyParam(Span),
1385-
TyParam(DefId),
1385+
Param(DefId),
13861386
DefVariant,
13871387
None,
13881388
}
@@ -1498,11 +1498,11 @@ fn generics_args_err_extend<'a>(
14981498
GenericsArgsErrExtend::DefVariant => {
14991499
err.note("enum variants can't have type parameters");
15001500
}
1501-
GenericsArgsErrExtend::TyParam(def_id) => {
1502-
if let Some(span) = tcx.def_ident_span(def_id) {
1503-
let name = tcx.item_name(def_id);
1504-
err.span_note(span, format!("type parameter `{name}` defined here"));
1505-
}
1501+
GenericsArgsErrExtend::Param(def_id) => {
1502+
let span = tcx.def_ident_span(def_id).unwrap();
1503+
let kind = tcx.def_descr(def_id);
1504+
let name = tcx.item_name(def_id);
1505+
err.span_note(span, format!("{kind} `{name}` defined here"));
15061506
}
15071507
GenericsArgsErrExtend::SelfTyParam(span) => {
15081508
err.span_suggestion_verbose(

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
17581758
assert_eq!(opt_self_ty, None);
17591759
let _ = self.prohibit_generic_args(
17601760
path.segments.iter(),
1761-
GenericsArgsErrExtend::TyParam(def_id),
1761+
GenericsArgsErrExtend::Param(def_id),
17621762
);
17631763
self.lower_ty_param(hir_id)
17641764
}
@@ -2191,10 +2191,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21912191

21922192
hir::ExprKind::Path(hir::QPath::Resolved(
21932193
_,
2194-
&hir::Path {
2195-
res: Res::Def(DefKind::ConstParam, def_id), ..
2194+
path @ &hir::Path {
2195+
res: Res::Def(DefKind::ConstParam, def_id),
2196+
..
21962197
},
21972198
)) => {
2199+
let _ = self.prohibit_generic_args(
2200+
path.segments.iter(),
2201+
GenericsArgsErrExtend::Param(def_id),
2202+
);
21982203
let ty = tcx
21992204
.type_of(def_id)
22002205
.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)