Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7442931

Browse files
committedNov 30, 2024
Auto merge of #133684 - RalfJung:rollup-j2tmrg7, r=RalfJung
Rollup of 6 pull requests Successful merges: - #131698 (use stores of the correct size to set discriminants) - #133571 (Mark visionOS as supporting `std`) - #133655 (Eliminate print_expr_maybe_paren function from pretty printers) - #133667 (Remove unused code) - #133670 (bump hashbrown version) - #133673 (replace hard coded error id with `ErrorKind::DirectoryNotEmpty`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f981b2e + bdb44d0 commit 7442931

File tree

10 files changed

+170
-68
lines changed

10 files changed

+170
-68
lines changed
 

‎compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+87-31
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ impl<'a> State<'a> {
5858
self.pclose()
5959
}
6060

61-
fn print_expr_maybe_paren(&mut self, expr: &ast::Expr, prec: i8, fixup: FixupContext) {
62-
self.print_expr_cond_paren(expr, expr.precedence() < prec, fixup);
63-
}
64-
6561
/// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
6662
/// `if cond { ... }`.
6763
fn print_expr_as_cond(&mut self, expr: &ast::Expr) {
@@ -237,7 +233,7 @@ impl<'a> State<'a> {
237233
// because the latter is valid syntax but with the incorrect meaning.
238234
// It's a match-expression followed by tuple-expression, not a function
239235
// call.
240-
self.print_expr_maybe_paren(func, prec, fixup.leftmost_subexpression());
236+
self.print_expr_cond_paren(func, func.precedence() < prec, fixup.leftmost_subexpression());
241237

242238
self.print_call_post(args)
243239
}
@@ -258,7 +254,11 @@ impl<'a> State<'a> {
258254
// boundaries, `$receiver.method()` can be parsed back as a statement
259255
// containing an expression if and only if `$receiver` can be parsed as
260256
// a statement containing an expression.
261-
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS, fixup);
257+
self.print_expr_cond_paren(
258+
receiver,
259+
receiver.precedence() < parser::PREC_UNAMBIGUOUS,
260+
fixup,
261+
);
262262

263263
self.word(".");
264264
self.print_ident(segment.ident);
@@ -306,17 +306,29 @@ impl<'a> State<'a> {
306306
_ => left_prec,
307307
};
308308

309-
self.print_expr_maybe_paren(lhs, left_prec, fixup.leftmost_subexpression());
309+
self.print_expr_cond_paren(
310+
lhs,
311+
lhs.precedence() < left_prec,
312+
fixup.leftmost_subexpression(),
313+
);
310314

311315
self.space();
312316
self.word_space(op.node.as_str());
313317

314-
self.print_expr_maybe_paren(rhs, right_prec, fixup.subsequent_subexpression());
318+
self.print_expr_cond_paren(
319+
rhs,
320+
rhs.precedence() < right_prec,
321+
fixup.subsequent_subexpression(),
322+
);
315323
}
316324

317325
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr, fixup: FixupContext) {
318326
self.word(op.as_str());
319-
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX, fixup.subsequent_subexpression());
327+
self.print_expr_cond_paren(
328+
expr,
329+
expr.precedence() < parser::PREC_PREFIX,
330+
fixup.subsequent_subexpression(),
331+
);
320332
}
321333

322334
fn print_expr_addr_of(
@@ -334,7 +346,11 @@ impl<'a> State<'a> {
334346
self.print_mutability(mutability, true);
335347
}
336348
}
337-
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX, fixup.subsequent_subexpression());
349+
self.print_expr_cond_paren(
350+
expr,
351+
expr.precedence() < parser::PREC_PREFIX,
352+
fixup.subsequent_subexpression(),
353+
);
338354
}
339355

340356
pub(super) fn print_expr(&mut self, expr: &ast::Expr, fixup: FixupContext) {
@@ -417,7 +433,11 @@ impl<'a> State<'a> {
417433
}
418434
ast::ExprKind::Cast(expr, ty) => {
419435
let prec = AssocOp::As.precedence() as i8;
420-
self.print_expr_maybe_paren(expr, prec, fixup.leftmost_subexpression());
436+
self.print_expr_cond_paren(
437+
expr,
438+
expr.precedence() < prec,
439+
fixup.leftmost_subexpression(),
440+
);
421441
self.space();
422442
self.word_space("as");
423443
self.print_type(ty);
@@ -490,7 +510,11 @@ impl<'a> State<'a> {
490510
self.space();
491511
}
492512
MatchKind::Postfix => {
493-
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
513+
self.print_expr_cond_paren(
514+
expr,
515+
expr.precedence() < parser::PREC_UNAMBIGUOUS,
516+
fixup,
517+
);
494518
self.word_nbsp(".match");
495519
}
496520
}
@@ -550,33 +574,57 @@ impl<'a> State<'a> {
550574
self.print_block_with_attrs(blk, attrs);
551575
}
552576
ast::ExprKind::Await(expr, _) => {
553-
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
577+
self.print_expr_cond_paren(
578+
expr,
579+
expr.precedence() < parser::PREC_UNAMBIGUOUS,
580+
fixup,
581+
);
554582
self.word(".await");
555583
}
556584
ast::ExprKind::Assign(lhs, rhs, _) => {
557585
let prec = AssocOp::Assign.precedence() as i8;
558-
self.print_expr_maybe_paren(lhs, prec + 1, fixup.leftmost_subexpression());
586+
self.print_expr_cond_paren(
587+
lhs,
588+
lhs.precedence() <= prec,
589+
fixup.leftmost_subexpression(),
590+
);
559591
self.space();
560592
self.word_space("=");
561-
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression());
593+
self.print_expr_cond_paren(
594+
rhs,
595+
rhs.precedence() < prec,
596+
fixup.subsequent_subexpression(),
597+
);
562598
}
563599
ast::ExprKind::AssignOp(op, lhs, rhs) => {
564600
let prec = AssocOp::Assign.precedence() as i8;
565-
self.print_expr_maybe_paren(lhs, prec + 1, fixup.leftmost_subexpression());
601+
self.print_expr_cond_paren(
602+
lhs,
603+
lhs.precedence() <= prec,
604+
fixup.leftmost_subexpression(),
605+
);
566606
self.space();
567607
self.word(op.node.as_str());
568608
self.word_space("=");
569-
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression());
609+
self.print_expr_cond_paren(
610+
rhs,
611+
rhs.precedence() < prec,
612+
fixup.subsequent_subexpression(),
613+
);
570614
}
571615
ast::ExprKind::Field(expr, ident) => {
572-
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
616+
self.print_expr_cond_paren(
617+
expr,
618+
expr.precedence() < parser::PREC_UNAMBIGUOUS,
619+
fixup,
620+
);
573621
self.word(".");
574622
self.print_ident(*ident);
575623
}
576624
ast::ExprKind::Index(expr, index, _) => {
577-
self.print_expr_maybe_paren(
625+
self.print_expr_cond_paren(
578626
expr,
579-
parser::PREC_UNAMBIGUOUS,
627+
expr.precedence() < parser::PREC_UNAMBIGUOUS,
580628
fixup.leftmost_subexpression(),
581629
);
582630
self.word("[");
@@ -590,14 +638,22 @@ impl<'a> State<'a> {
590638
// a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
591639
let fake_prec = AssocOp::LOr.precedence() as i8;
592640
if let Some(e) = start {
593-
self.print_expr_maybe_paren(e, fake_prec, fixup.leftmost_subexpression());
641+
self.print_expr_cond_paren(
642+
e,
643+
e.precedence() < fake_prec,
644+
fixup.leftmost_subexpression(),
645+
);
594646
}
595647
match limits {
596648
ast::RangeLimits::HalfOpen => self.word(".."),
597649
ast::RangeLimits::Closed => self.word("..="),
598650
}
599651
if let Some(e) = end {
600-
self.print_expr_maybe_paren(e, fake_prec, fixup.subsequent_subexpression());
652+
self.print_expr_cond_paren(
653+
e,
654+
e.precedence() < fake_prec,
655+
fixup.subsequent_subexpression(),
656+
);
601657
}
602658
}
603659
ast::ExprKind::Underscore => self.word("_"),
@@ -632,9 +688,9 @@ impl<'a> State<'a> {
632688
self.word("return");
633689
if let Some(expr) = result {
634690
self.word(" ");
635-
self.print_expr_maybe_paren(
691+
self.print_expr_cond_paren(
636692
expr,
637-
parser::PREC_JUMP,
693+
expr.precedence() < parser::PREC_JUMP,
638694
fixup.subsequent_subexpression(),
639695
);
640696
}
@@ -645,19 +701,19 @@ impl<'a> State<'a> {
645701
self.word("yeet");
646702
if let Some(expr) = result {
647703
self.word(" ");
648-
self.print_expr_maybe_paren(
704+
self.print_expr_cond_paren(
649705
expr,
650-
parser::PREC_JUMP,
706+
expr.precedence() < parser::PREC_JUMP,
651707
fixup.subsequent_subexpression(),
652708
);
653709
}
654710
}
655711
ast::ExprKind::Become(result) => {
656712
self.word("become");
657713
self.word(" ");
658-
self.print_expr_maybe_paren(
714+
self.print_expr_cond_paren(
659715
result,
660-
parser::PREC_JUMP,
716+
result.precedence() < parser::PREC_JUMP,
661717
fixup.subsequent_subexpression(),
662718
);
663719
}
@@ -709,15 +765,15 @@ impl<'a> State<'a> {
709765

710766
if let Some(expr) = e {
711767
self.space();
712-
self.print_expr_maybe_paren(
768+
self.print_expr_cond_paren(
713769
expr,
714-
parser::PREC_JUMP,
770+
expr.precedence() < parser::PREC_JUMP,
715771
fixup.subsequent_subexpression(),
716772
);
717773
}
718774
}
719775
ast::ExprKind::Try(e) => {
720-
self.print_expr_maybe_paren(e, parser::PREC_UNAMBIGUOUS, fixup);
776+
self.print_expr_cond_paren(e, e.precedence() < parser::PREC_UNAMBIGUOUS, fixup);
721777
self.word("?")
722778
}
723779
ast::ExprKind::TryBlock(blk) => {

‎compiler/rustc_codegen_ssa/src/mir/place.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_abi::Primitive::{Int, Pointer};
2-
use rustc_abi::{Align, FieldsShape, Size, TagEncoding, VariantIdx, Variants};
2+
use rustc_abi::{Align, BackendRepr, FieldsShape, Size, TagEncoding, VariantIdx, Variants};
3+
use rustc_middle::mir::interpret::Scalar;
34
use rustc_middle::mir::tcx::PlaceTy;
45
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
56
use rustc_middle::ty::{self, Ty};
@@ -385,15 +386,22 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
385386
if variant_index != untagged_variant {
386387
let niche = self.project_field(bx, tag_field);
387388
let niche_llty = bx.cx().immediate_backend_type(niche.layout);
389+
let BackendRepr::Scalar(scalar) = niche.layout.backend_repr else {
390+
bug!("expected a scalar placeref for the niche");
391+
};
392+
// We are supposed to compute `niche_value.wrapping_add(niche_start)` wrapping
393+
// around the `niche`'s type.
394+
// The easiest way to do that is to do wrapping arithmetic on `u128` and then
395+
// masking off any extra bits that occur because we did the arithmetic with too many bits.
388396
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
389397
let niche_value = (niche_value as u128).wrapping_add(niche_start);
390-
// FIXME(eddyb): check the actual primitive type here.
391-
let niche_llval = if niche_value == 0 {
392-
// HACK(eddyb): using `c_null` as it works on all types.
393-
bx.cx().const_null(niche_llty)
394-
} else {
395-
bx.cx().const_uint_big(niche_llty, niche_value)
396-
};
398+
let niche_value = niche_value & niche.layout.size.unsigned_int_max();
399+
400+
let niche_llval = bx.cx().scalar_to_backend(
401+
Scalar::from_uint(niche_value, niche.layout.size),
402+
scalar,
403+
niche_llty,
404+
);
397405
OperandValue::Immediate(niche_llval).store(bx, niche);
398406
}
399407
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.