Skip to content

Commit b025089

Browse files
committed
Prevent generic pattern types from being used in libstd
1 parent 8df89d1 commit b025089

File tree

7 files changed

+105
-10
lines changed

7 files changed

+105
-10
lines changed

compiler/rustc_feature/src/unstable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ declare_features! (
507507
(incomplete, generic_const_exprs, "1.56.0", Some(76560)),
508508
/// Allows generic parameters and where-clauses on free & associated const items.
509509
(incomplete, generic_const_items, "1.73.0", Some(113521)),
510+
/// Allows any generic constants being used as pattern type range ends
511+
(incomplete, generic_pattern_types, "CURRENT_RUSTC_VERSION", Some(136574)),
512+
/// Allows using `#[prelude_import]` on glob `use` items.
510513
/// Allows registering static items globally, possibly across crates, to iterate over at runtime.
511514
(unstable, global_registration, "1.80.0", Some(125119)),
512515
/// Allows using guards in patterns.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ symbols! {
10131013
generic_const_exprs,
10141014
generic_const_items,
10151015
generic_param_attrs,
1016+
generic_pattern_types,
10161017
get_context,
10171018
global_alloc_ty,
10181019
global_allocator,

compiler/rustc_trait_selection/src/traits/wf.rs

+46-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,51 @@ 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+
let c = self.tcx().normalize_erasing_regions(
725+
self.infcx.typing_env(self.param_env),
726+
c,
727+
);
728+
if c.has_aliases() || c.has_param() {
729+
if self.span.is_dummy() {
730+
self.tcx().dcx().delayed_bug(
731+
"feature error should be reported elsewhere, too",
732+
);
733+
} else {
734+
feature_err(
735+
&self.tcx().sess,
736+
sym::generic_pattern_types,
737+
self.span,
738+
"wraparound pattern type ranges cause monomorphization time errors",
739+
)
740+
.emit();
741+
}
742+
}
743+
}
744+
};
745+
if let Some(start) = start {
746+
check(start)
747+
}
748+
if let Some(end) = end {
749+
check(end)
750+
}
751+
}
752+
}
709753
}
710754

711755
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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)]
55

66
//@ revisions: default const_arg
@@ -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)