Skip to content

Commit 7450913

Browse files
committed
Auto merge of rust-lang#140474 - tgross35:rollup-rdkao89, r=tgross35
Rollup of 7 pull requests Successful merges: - rust-lang#138344 (Enable `reliable_f16_math` on x86) - rust-lang#139909 (implement or-patterns for pattern types) - rust-lang#140392 (compiletest: Remove the libtest-based executor and its dependency) - rust-lang#140400 (PassWrapper: adapt for llvm/llvm-project@d3d856ad8469) - rust-lang#140422 (unwind: bump `unwinding` dependency to 0.2.6) - rust-lang#140432 (Update documentation for `fn target_config`) - rust-lang#140433 (Replace the \01__gnu_mcount_nc to LLVM intrinsic for additional ARM targets) r? `@ghost` `@rustbot` modify labels: rollup
2 parents efcbb94 + ff6a980 commit 7450913

File tree

47 files changed

+691
-274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+691
-274
lines changed

compiler/rustc_ast/src/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,8 @@ pub enum TyPatKind {
24692469
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
24702470
Range(Option<P<AnonConst>>, Option<P<AnonConst>>, Spanned<RangeEnd>),
24712471

2472+
Or(ThinVec<P<TyPat>>),
2473+
24722474
/// Placeholder for a pattern that wasn't syntactically well formed in some way.
24732475
Err(ErrorGuaranteed),
24742476
}

compiler/rustc_ast/src/mut_visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut P<TyPat>) {
612612
visit_opt(start, |c| vis.visit_anon_const(c));
613613
visit_opt(end, |c| vis.visit_anon_const(c));
614614
}
615+
TyPatKind::Or(variants) => visit_thin_vec(variants, |p| vis.visit_ty_pat(p)),
615616
TyPatKind::Err(_) => {}
616617
}
617618
visit_lazy_tts(vis, tokens);

compiler/rustc_ast/src/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ pub fn walk_ty_pat<'a, V: Visitor<'a>>(visitor: &mut V, tp: &'a TyPat) -> V::Res
608608
visit_opt!(visitor, visit_anon_const, start);
609609
visit_opt!(visitor, visit_anon_const, end);
610610
}
611+
TyPatKind::Or(variants) => walk_list!(visitor, visit_ty_pat, variants),
611612
TyPatKind::Err(_) => {}
612613
}
613614
V::Result::output()

compiler/rustc_ast_lowering/src/pat.rs

+5
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
464464
)
465465
}),
466466
),
467+
TyPatKind::Or(variants) => {
468+
hir::TyPatKind::Or(self.arena.alloc_from_iter(
469+
variants.iter().map(|pat| self.lower_ty_pat_mut(pat, base_type)),
470+
))
471+
}
467472
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
468473
};
469474

compiler/rustc_ast_pretty/src/pprust/state.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,17 @@ impl<'a> State<'a> {
11581158
self.print_expr_anon_const(end, &[]);
11591159
}
11601160
}
1161+
rustc_ast::TyPatKind::Or(variants) => {
1162+
let mut first = true;
1163+
for pat in variants {
1164+
if first {
1165+
first = false
1166+
} else {
1167+
self.word(" | ");
1168+
}
1169+
self.print_ty_pat(pat);
1170+
}
1171+
}
11611172
rustc_ast::TyPatKind::Err(_) => {
11621173
self.popen();
11631174
self.word("/*ERROR*/");

compiler/rustc_builtin_macros/src/pattern_type.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc_ast::ptr::P;
22
use rustc_ast::tokenstream::TokenStream;
3-
use rustc_ast::{AnonConst, DUMMY_NODE_ID, Ty, TyPat, TyPatKind, ast};
3+
use rustc_ast::{AnonConst, DUMMY_NODE_ID, Ty, TyPat, TyPatKind, ast, token};
44
use rustc_errors::PResult;
55
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
66
use rustc_parse::exp;
7+
use rustc_parse::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
78
use rustc_span::Span;
89

910
pub(crate) fn expand<'cx>(
@@ -26,19 +27,42 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2627

2728
let ty = parser.parse_ty()?;
2829
parser.expect_keyword(exp!(Is))?;
29-
let pat = parser.parse_pat_no_top_alt(None, None)?.into_inner();
3030

31+
let pat = pat_to_ty_pat(
32+
cx,
33+
parser
34+
.parse_pat_no_top_guard(
35+
None,
36+
RecoverComma::No,
37+
RecoverColon::No,
38+
CommaRecoveryMode::EitherTupleOrPipe,
39+
)?
40+
.into_inner(),
41+
);
42+
43+
if parser.token != token::Eof {
44+
parser.unexpected()?;
45+
}
46+
47+
Ok((ty, pat))
48+
}
49+
50+
fn ty_pat(kind: TyPatKind, span: Span) -> P<TyPat> {
51+
P(TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None })
52+
}
53+
54+
fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> P<TyPat> {
3155
let kind = match pat.kind {
3256
ast::PatKind::Range(start, end, include_end) => TyPatKind::Range(
3357
start.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
3458
end.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
3559
include_end,
3660
),
61+
ast::PatKind::Or(variants) => TyPatKind::Or(
62+
variants.into_iter().map(|pat| pat_to_ty_pat(cx, pat.into_inner())).collect(),
63+
),
3764
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
3865
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),
3966
};
40-
41-
let pat = P(TyPat { id: pat.id, kind, span: pat.span, tokens: pat.tokens });
42-
43-
Ok((ty, pat))
67+
ty_pat(kind, pat.span)
4468
}

compiler/rustc_codegen_llvm/src/llvm_util.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,9 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
466466
_ => true,
467467
};
468468

469-
cfg.has_reliable_f16_math = match (target_arch, target_os) {
470-
// x86 has a crash for `powi`: <https://github.com/llvm/llvm-project/issues/105747>
471-
("x86" | "x86_64", _) => false,
472-
// Assume that working `f16` means working `f16` math for most platforms, since
473-
// operations just go through `f32`.
474-
_ => true,
475-
} && cfg.has_reliable_f16;
469+
// Assume that working `f16` means working `f16` math for most platforms, since
470+
// operations just go through `f32`.
471+
cfg.has_reliable_f16_math = cfg.has_reliable_f16;
476472

477473
cfg.has_reliable_f128_math = match (target_arch, target_os) {
478474
// LLVM lowers `fp128` math to `long double` symbols even on platforms where

compiler/rustc_codegen_ssa/src/traits/backend.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ pub trait CodegenBackend {
4545

4646
fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
4747

48-
/// Returns two feature sets:
49-
/// - The first has the features that should be set in `cfg(target_features)`.
50-
/// - The second is like the first, but also includes unstable features.
51-
///
52-
/// RUSTC_SPECIFIC_FEATURES should be skipped here, those are handled outside codegen.
48+
/// Collect target-specific options that should be set in `cfg(...)`, including
49+
/// `target_feature` and support for unstable float types.
5350
fn target_config(&self, _sess: &Session) -> TargetConfig {
5451
TargetConfig {
5552
target_features: vec![],
5653
unstable_target_features: vec![],
54+
// `true` is used as a default so backends need to acknowledge when they do not
55+
// support the float types, rather than accidentally quietly skipping all tests.
5756
has_reliable_f16: true,
5857
has_reliable_f16_math: true,
5958
has_reliable_f128: true,

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,21 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
6161
ensure_monomorphic_enough(tcx, tp_ty)?;
6262
ConstValue::from_u128(tcx.type_id_hash(tp_ty).as_u128())
6363
}
64-
sym::variant_count => match tp_ty.kind() {
64+
sym::variant_count => match match tp_ty.kind() {
65+
// Pattern types have the same number of variants as their base type.
66+
// Even if we restrict e.g. which variants are valid, the variants are essentially just uninhabited.
67+
// And `Result<(), !>` still has two variants according to `variant_count`.
68+
ty::Pat(base, _) => *base,
69+
_ => tp_ty,
70+
}
71+
.kind()
72+
{
6573
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
6674
ty::Adt(adt, _) => ConstValue::from_target_usize(adt.variants().len() as u64, &tcx),
6775
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
6876
throw_inval!(TooGeneric)
6977
}
70-
ty::Pat(_, pat) => match **pat {
71-
ty::PatternKind::Range { .. } => ConstValue::from_target_usize(0u64, &tcx),
72-
// Future pattern kinds may have more variants
73-
},
78+
ty::Pat(..) => unreachable!(),
7479
ty::Bound(_, _) => bug!("bound ty during ctfe"),
7580
ty::Bool
7681
| ty::Char

compiler/rustc_const_eval/src/interpret/validity.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,14 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12481248
// Range patterns are precisely reflected into `valid_range` and thus
12491249
// handled fully by `visit_scalar` (called below).
12501250
ty::PatternKind::Range { .. } => {},
1251+
1252+
// FIXME(pattern_types): check that the value is covered by one of the variants.
1253+
// For now, we rely on layout computation setting the scalar's `valid_range` to
1254+
// match the pattern. However, this cannot always work; the layout may
1255+
// pessimistically cover actually illegal ranges and Miri would miss that UB.
1256+
// The consolation here is that codegen also will miss that UB, so at least
1257+
// we won't see optimizations actually breaking such programs.
1258+
ty::PatternKind::Or(_patterns) => {}
12511259
}
12521260
}
12531261
_ => {

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,9 @@ pub enum TyPatKind<'hir> {
18131813
/// A range pattern (e.g., `1..=2` or `1..2`).
18141814
Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
18151815

1816+
/// A list of patterns where only one needs to be satisfied
1817+
Or(&'hir [TyPat<'hir>]),
1818+
18161819
/// A placeholder for a pattern that wasn't well formed in some way.
18171820
Err(ErrorGuaranteed),
18181821
}

compiler/rustc_hir/src/intravisit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>)
710710
try_visit!(visitor.visit_const_arg_unambig(lower_bound));
711711
try_visit!(visitor.visit_const_arg_unambig(upper_bound));
712712
}
713+
TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
713714
TyPatKind::Err(_) => (),
714715
}
715716
V::Result::output()

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: S
9494
}
9595

9696
Node::TyPat(pat) => {
97-
let hir::TyKind::Pat(ty, p) = tcx.parent_hir_node(pat.hir_id).expect_ty().kind else {
98-
bug!()
97+
let node = match tcx.parent_hir_node(pat.hir_id) {
98+
// Or patterns can be nested one level deep
99+
Node::TyPat(p) => tcx.parent_hir_node(p.hir_id),
100+
other => other,
99101
};
100-
assert_eq!(p.hir_id, pat.hir_id);
102+
let hir::TyKind::Pat(ty, _) = node.expect_ty().kind else { bug!() };
101103
icx.lower_ty(ty)
102104
}
103105

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+36-24
Original file line numberDiff line numberDiff line change
@@ -2715,30 +2715,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27152715
hir::TyKind::Pat(ty, pat) => {
27162716
let ty_span = ty.span;
27172717
let ty = self.lower_ty(ty);
2718-
let pat_ty = match pat.kind {
2719-
hir::TyPatKind::Range(start, end) => {
2720-
let (ty, start, end) = match ty.kind() {
2721-
// Keep this list of types in sync with the list of types that
2722-
// the `RangePattern` trait is implemented for.
2723-
ty::Int(_) | ty::Uint(_) | ty::Char => {
2724-
let start = self.lower_const_arg(start, FeedConstTy::No);
2725-
let end = self.lower_const_arg(end, FeedConstTy::No);
2726-
(ty, start, end)
2727-
}
2728-
_ => {
2729-
let guar = self.dcx().span_delayed_bug(
2730-
ty_span,
2731-
"invalid base type for range pattern",
2732-
);
2733-
let errc = ty::Const::new_error(tcx, guar);
2734-
(Ty::new_error(tcx, guar), errc, errc)
2735-
}
2736-
};
2737-
2738-
let pat = tcx.mk_pat(ty::PatternKind::Range { start, end });
2739-
Ty::new_pat(tcx, ty, pat)
2740-
}
2741-
hir::TyPatKind::Err(e) => Ty::new_error(tcx, e),
2718+
let pat_ty = match self.lower_pat_ty_pat(ty, ty_span, pat) {
2719+
Ok(kind) => Ty::new_pat(tcx, ty, tcx.mk_pat(kind)),
2720+
Err(guar) => Ty::new_error(tcx, guar),
27422721
};
27432722
self.record_ty(pat.hir_id, ty, pat.span);
27442723
pat_ty
@@ -2750,6 +2729,39 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27502729
result_ty
27512730
}
27522731

2732+
fn lower_pat_ty_pat(
2733+
&self,
2734+
ty: Ty<'tcx>,
2735+
ty_span: Span,
2736+
pat: &hir::TyPat<'tcx>,
2737+
) -> Result<ty::PatternKind<'tcx>, ErrorGuaranteed> {
2738+
let tcx = self.tcx();
2739+
match pat.kind {
2740+
hir::TyPatKind::Range(start, end) => {
2741+
match ty.kind() {
2742+
// Keep this list of types in sync with the list of types that
2743+
// the `RangePattern` trait is implemented for.
2744+
ty::Int(_) | ty::Uint(_) | ty::Char => {
2745+
let start = self.lower_const_arg(start, FeedConstTy::No);
2746+
let end = self.lower_const_arg(end, FeedConstTy::No);
2747+
Ok(ty::PatternKind::Range { start, end })
2748+
}
2749+
_ => Err(self
2750+
.dcx()
2751+
.span_delayed_bug(ty_span, "invalid base type for range pattern")),
2752+
}
2753+
}
2754+
hir::TyPatKind::Or(patterns) => {
2755+
self.tcx()
2756+
.mk_patterns_from_iter(patterns.iter().map(|pat| {
2757+
self.lower_pat_ty_pat(ty, ty_span, pat).map(|pat| tcx.mk_pat(pat))
2758+
}))
2759+
.map(ty::PatternKind::Or)
2760+
}
2761+
hir::TyPatKind::Err(e) => Err(e),
2762+
}
2763+
}
2764+
27532765
/// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
27542766
#[instrument(level = "debug", skip(self), ret)]
27552767
fn lower_opaque_ty(&self, def_id: LocalDefId, in_trait: bool) -> Ty<'tcx> {

compiler/rustc_hir_analysis/src/variance/constraints.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
251251
}
252252

253253
ty::Pat(typ, pat) => {
254-
match *pat {
255-
ty::PatternKind::Range { start, end } => {
256-
self.add_constraints_from_const(current, start, variance);
257-
self.add_constraints_from_const(current, end, variance);
258-
}
259-
}
254+
self.add_constraints_from_pat(current, variance, pat);
260255
self.add_constraints_from_ty(current, typ, variance);
261256
}
262257

@@ -334,6 +329,25 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
334329
}
335330
}
336331

332+
fn add_constraints_from_pat(
333+
&mut self,
334+
current: &CurrentItem,
335+
variance: VarianceTermPtr<'a>,
336+
pat: ty::Pattern<'tcx>,
337+
) {
338+
match *pat {
339+
ty::PatternKind::Range { start, end } => {
340+
self.add_constraints_from_const(current, start, variance);
341+
self.add_constraints_from_const(current, end, variance);
342+
}
343+
ty::PatternKind::Or(patterns) => {
344+
for pat in patterns {
345+
self.add_constraints_from_pat(current, variance, pat)
346+
}
347+
}
348+
}
349+
}
350+
337351
/// Adds constraints appropriate for a nominal type (enum, struct,
338352
/// object, etc) appearing in a context with ambient variance `variance`
339353
fn add_constraints_from_args(

compiler/rustc_hir_pretty/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,19 @@ impl<'a> State<'a> {
18821882
self.word("..=");
18831883
self.print_const_arg(end);
18841884
}
1885+
TyPatKind::Or(patterns) => {
1886+
self.popen();
1887+
let mut first = true;
1888+
for pat in patterns {
1889+
if first {
1890+
first = false;
1891+
} else {
1892+
self.word(" | ");
1893+
}
1894+
self.print_ty_pat(pat);
1895+
}
1896+
self.pclose();
1897+
}
18851898
TyPatKind::Err(_) => {
18861899
self.popen();
18871900
self.word("/*ERROR*/");

0 commit comments

Comments
 (0)