Skip to content

Commit 6008d73

Browse files
authored
Unrolled build for rust-lang#136584
Rollup merge of rust-lang#136584 - oli-obk:pattern-types-generic, r=BoxyUwU Prevent generic pattern types from being used in libstd Pattern types should follow the same rules that patterns follow. So a pattern type range must not wrap and not be empty. While we reject such invalid ranges at layout computation time, that only happens during monomorphization in the case of const generics. This is the exact same issue as other const generic math has, and since there's no solution there yet, I put these pattern types behind a separate incomplete feature. These are not necessary for the pattern types MVP (replacing the layout range attributes in libcore and rustc). cc rust-lang#136574 (new tracking issue for the `generic_pattern_types` feature gate) r? ``@lcnr`` cc ``@scottmcm`` ``@joshtriplett``
2 parents ffa9afe + fab6d8a commit 6008d73

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ declare_features! (
513513
(incomplete, generic_const_exprs, "1.56.0", Some(76560)),
514514
/// Allows generic parameters and where-clauses on free & associated const items.
515515
(incomplete, generic_const_items, "1.73.0", Some(113521)),
516+
/// Allows any generic constants being used as pattern type range ends
517+
(incomplete, generic_pattern_types, "CURRENT_RUSTC_VERSION", Some(136574)),
516518
/// Allows registering static items globally, possibly across crates, to iterate over at runtime.
517519
(unstable, global_registration, "1.80.0", Some(125119)),
518520
/// Allows using guards in patterns.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ symbols! {
10241024
generic_const_exprs,
10251025
generic_const_items,
10261026
generic_param_attrs,
1027+
generic_pattern_types,
10271028
get_context,
10281029
global_alloc_ty,
10291030
global_allocator,

compiler/rustc_trait_selection/src/traits/wf.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use rustc_middle::ty::{
88
self, GenericArg, GenericArgKind, GenericArgsRef, Ty, TyCtxt, TypeSuperVisitable,
99
TypeVisitable, TypeVisitableExt, TypeVisitor,
1010
};
11-
use rustc_span::Span;
11+
use rustc_session::parse::feature_err;
1212
use rustc_span::def_id::{DefId, LocalDefId};
13+
use rustc_span::{Span, sym};
1314
use tracing::{debug, instrument, trace};
1415

1516
use crate::infer::InferCtxt;
@@ -704,8 +705,47 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
704705
));
705706
}
706707

707-
ty::Pat(subty, _) => {
708+
ty::Pat(subty, pat) => {
708709
self.require_sized(subty, ObligationCauseCode::Misc);
710+
match *pat {
711+
ty::PatternKind::Range { start, end, include_end: _ } => {
712+
let mut check = |c| {
713+
let cause = self.cause(ObligationCauseCode::Misc);
714+
self.out.push(traits::Obligation::with_depth(
715+
tcx,
716+
cause.clone(),
717+
self.recursion_depth,
718+
self.param_env,
719+
ty::Binder::dummy(ty::PredicateKind::Clause(
720+
ty::ClauseKind::ConstArgHasType(c, subty),
721+
)),
722+
));
723+
if !tcx.features().generic_pattern_types() {
724+
if c.has_param() {
725+
if self.span.is_dummy() {
726+
self.tcx().dcx().delayed_bug(
727+
"feature error should be reported elsewhere, too",
728+
);
729+
} else {
730+
feature_err(
731+
&self.tcx().sess,
732+
sym::generic_pattern_types,
733+
self.span,
734+
"wraparound pattern type ranges cause monomorphization time errors",
735+
)
736+
.emit();
737+
}
738+
}
739+
}
740+
};
741+
if let Some(start) = start {
742+
check(start)
743+
}
744+
if let Some(end) = end {
745+
check(end)
746+
}
747+
}
748+
}
709749
}
710750

711751
ty::Tuple(tys) => {

tests/ui/type/pattern_types/assoc_const.default.stderr

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
error[E0658]: wraparound pattern type ranges cause monomorphization time errors
2+
--> $DIR/assoc_const.rs:17:19
3+
|
4+
LL | fn foo<T: Foo>(_: pattern_type!(u32 is <T as Foo>::START..=<T as Foo>::END)) {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #136574 <https://github.com/rust-lang/rust/issues/136574> for more information
8+
= help: add `#![feature(generic_pattern_types)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: wraparound pattern type ranges cause monomorphization time errors
12+
--> $DIR/assoc_const.rs:17:19
13+
|
14+
LL | fn foo<T: Foo>(_: pattern_type!(u32 is <T as Foo>::START..=<T as Foo>::END)) {}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #136574 <https://github.com/rust-lang/rust/issues/136574> for more information
18+
= help: add `#![feature(generic_pattern_types)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
21+
122
error: constant expression depends on a generic parameter
223
--> $DIR/assoc_const.rs:17:19
324
|
@@ -15,22 +36,44 @@ LL | fn foo<T: Foo>(_: pattern_type!(u32 is <T as Foo>::START..=<T as Foo>::END)
1536
= note: this may fail depending on what value the parameter takes
1637
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1738

39+
error[E0658]: wraparound pattern type ranges cause monomorphization time errors
40+
--> $DIR/assoc_const.rs:22:19
41+
|
42+
LL | fn bar<T: Foo>(_: pattern_type!(u32 is T::START..=T::END)) {}
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
= note: see issue #136574 <https://github.com/rust-lang/rust/issues/136574> for more information
46+
= help: add `#![feature(generic_pattern_types)]` to the crate attributes to enable
47+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
48+
49+
error[E0658]: wraparound pattern type ranges cause monomorphization time errors
50+
--> $DIR/assoc_const.rs:22:19
51+
|
52+
LL | fn bar<T: Foo>(_: pattern_type!(u32 is T::START..=T::END)) {}
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
|
55+
= note: see issue #136574 <https://github.com/rust-lang/rust/issues/136574> for more information
56+
= help: add `#![feature(generic_pattern_types)]` to the crate attributes to enable
57+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
58+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
59+
1860
error: constant expression depends on a generic parameter
19-
--> $DIR/assoc_const.rs:20:19
61+
--> $DIR/assoc_const.rs:22:19
2062
|
2163
LL | fn bar<T: Foo>(_: pattern_type!(u32 is T::START..=T::END)) {}
2264
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2365
|
2466
= note: this may fail depending on what value the parameter takes
2567

2668
error: constant expression depends on a generic parameter
27-
--> $DIR/assoc_const.rs:20:19
69+
--> $DIR/assoc_const.rs:22:19
2870
|
2971
LL | fn bar<T: Foo>(_: pattern_type!(u32 is T::START..=T::END)) {}
3072
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3173
|
3274
= note: this may fail depending on what value the parameter takes
3375
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3476

35-
error: aborting due to 4 previous errors
77+
error: aborting due to 8 previous errors
3678

79+
For more information about this error, try `rustc --explain E0658`.

tests/ui/type/pattern_types/assoc_const.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![feature(pattern_types)]
22
#![feature(pattern_type_macro)]
3-
#![cfg_attr(const_arg, feature(generic_const_exprs))]
3+
#![cfg_attr(const_arg, feature(generic_const_exprs, generic_pattern_types))]
44
#![expect(incomplete_features)]
5-
5+
// gate-test-generic_pattern_types line to the test file.
66
//@ revisions: default const_arg
77

88
//@[const_arg] check-pass
@@ -17,8 +17,12 @@ trait Foo {
1717
fn foo<T: Foo>(_: pattern_type!(u32 is <T as Foo>::START..=<T as Foo>::END)) {}
1818
//[default]~^ ERROR: constant expression depends on a generic parameter
1919
//[default]~| ERROR: constant expression depends on a generic parameter
20+
//[default]~| ERROR: wraparound pattern type ranges cause monomorphization time errors
21+
//[default]~| ERROR: wraparound pattern type ranges cause monomorphization time errors
2022
fn bar<T: Foo>(_: pattern_type!(u32 is T::START..=T::END)) {}
2123
//[default]~^ ERROR: constant expression depends on a generic parameter
2224
//[default]~| ERROR: constant expression depends on a generic parameter
25+
//[default]~| ERROR: wraparound pattern type ranges cause monomorphization time errors
26+
//[default]~| ERROR: wraparound pattern type ranges cause monomorphization time errors
2327

2428
fn main() {}

tests/ui/type/pattern_types/const_generics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ check-pass
22

3-
#![feature(pattern_types)]
4-
#![feature(pattern_type_macro)]
3+
#![feature(pattern_types, generic_pattern_types, pattern_type_macro)]
4+
#![expect(incomplete_features)]
55

66
use std::pat::pattern_type;
77

tests/ui/type/pattern_types/transmute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#![feature(pattern_types)]
2-
#![feature(pattern_type_macro)]
1+
#![feature(pattern_types, pattern_type_macro, generic_pattern_types)]
2+
#![expect(incomplete_features)]
33

44
use std::pat::pattern_type;
55

0 commit comments

Comments
 (0)