1
Fork 0

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
This commit is contained in:
bors 2024-11-30 21:08:45 +00:00
commit 7442931d49
10 changed files with 170 additions and 68 deletions

View file

@ -58,10 +58,6 @@ impl<'a> State<'a> {
self.pclose() self.pclose()
} }
fn print_expr_maybe_paren(&mut self, expr: &ast::Expr, prec: i8, fixup: FixupContext) {
self.print_expr_cond_paren(expr, expr.precedence() < prec, fixup);
}
/// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
/// `if cond { ... }`. /// `if cond { ... }`.
fn print_expr_as_cond(&mut self, expr: &ast::Expr) { fn print_expr_as_cond(&mut self, expr: &ast::Expr) {
@ -237,7 +233,7 @@ impl<'a> State<'a> {
// because the latter is valid syntax but with the incorrect meaning. // because the latter is valid syntax but with the incorrect meaning.
// It's a match-expression followed by tuple-expression, not a function // It's a match-expression followed by tuple-expression, not a function
// call. // call.
self.print_expr_maybe_paren(func, prec, fixup.leftmost_subexpression()); self.print_expr_cond_paren(func, func.precedence() < prec, fixup.leftmost_subexpression());
self.print_call_post(args) self.print_call_post(args)
} }
@ -258,7 +254,11 @@ impl<'a> State<'a> {
// boundaries, `$receiver.method()` can be parsed back as a statement // boundaries, `$receiver.method()` can be parsed back as a statement
// containing an expression if and only if `$receiver` can be parsed as // containing an expression if and only if `$receiver` can be parsed as
// a statement containing an expression. // a statement containing an expression.
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS, fixup); self.print_expr_cond_paren(
receiver,
receiver.precedence() < parser::PREC_UNAMBIGUOUS,
fixup,
);
self.word("."); self.word(".");
self.print_ident(segment.ident); self.print_ident(segment.ident);
@ -306,17 +306,29 @@ impl<'a> State<'a> {
_ => left_prec, _ => left_prec,
}; };
self.print_expr_maybe_paren(lhs, left_prec, fixup.leftmost_subexpression()); self.print_expr_cond_paren(
lhs,
lhs.precedence() < left_prec,
fixup.leftmost_subexpression(),
);
self.space(); self.space();
self.word_space(op.node.as_str()); self.word_space(op.node.as_str());
self.print_expr_maybe_paren(rhs, right_prec, fixup.subsequent_subexpression()); self.print_expr_cond_paren(
rhs,
rhs.precedence() < right_prec,
fixup.subsequent_subexpression(),
);
} }
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr, fixup: FixupContext) { fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr, fixup: FixupContext) {
self.word(op.as_str()); self.word(op.as_str());
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX, fixup.subsequent_subexpression()); self.print_expr_cond_paren(
expr,
expr.precedence() < parser::PREC_PREFIX,
fixup.subsequent_subexpression(),
);
} }
fn print_expr_addr_of( fn print_expr_addr_of(
@ -334,7 +346,11 @@ impl<'a> State<'a> {
self.print_mutability(mutability, true); self.print_mutability(mutability, true);
} }
} }
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX, fixup.subsequent_subexpression()); self.print_expr_cond_paren(
expr,
expr.precedence() < parser::PREC_PREFIX,
fixup.subsequent_subexpression(),
);
} }
pub(super) fn print_expr(&mut self, expr: &ast::Expr, fixup: FixupContext) { pub(super) fn print_expr(&mut self, expr: &ast::Expr, fixup: FixupContext) {
@ -417,7 +433,11 @@ impl<'a> State<'a> {
} }
ast::ExprKind::Cast(expr, ty) => { ast::ExprKind::Cast(expr, ty) => {
let prec = AssocOp::As.precedence() as i8; let prec = AssocOp::As.precedence() as i8;
self.print_expr_maybe_paren(expr, prec, fixup.leftmost_subexpression()); self.print_expr_cond_paren(
expr,
expr.precedence() < prec,
fixup.leftmost_subexpression(),
);
self.space(); self.space();
self.word_space("as"); self.word_space("as");
self.print_type(ty); self.print_type(ty);
@ -490,7 +510,11 @@ impl<'a> State<'a> {
self.space(); self.space();
} }
MatchKind::Postfix => { MatchKind::Postfix => {
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup); self.print_expr_cond_paren(
expr,
expr.precedence() < parser::PREC_UNAMBIGUOUS,
fixup,
);
self.word_nbsp(".match"); self.word_nbsp(".match");
} }
} }
@ -550,33 +574,57 @@ impl<'a> State<'a> {
self.print_block_with_attrs(blk, attrs); self.print_block_with_attrs(blk, attrs);
} }
ast::ExprKind::Await(expr, _) => { ast::ExprKind::Await(expr, _) => {
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup); self.print_expr_cond_paren(
expr,
expr.precedence() < parser::PREC_UNAMBIGUOUS,
fixup,
);
self.word(".await"); self.word(".await");
} }
ast::ExprKind::Assign(lhs, rhs, _) => { ast::ExprKind::Assign(lhs, rhs, _) => {
let prec = AssocOp::Assign.precedence() as i8; let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1, fixup.leftmost_subexpression()); self.print_expr_cond_paren(
lhs,
lhs.precedence() <= prec,
fixup.leftmost_subexpression(),
);
self.space(); self.space();
self.word_space("="); self.word_space("=");
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression()); self.print_expr_cond_paren(
rhs,
rhs.precedence() < prec,
fixup.subsequent_subexpression(),
);
} }
ast::ExprKind::AssignOp(op, lhs, rhs) => { ast::ExprKind::AssignOp(op, lhs, rhs) => {
let prec = AssocOp::Assign.precedence() as i8; let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1, fixup.leftmost_subexpression()); self.print_expr_cond_paren(
lhs,
lhs.precedence() <= prec,
fixup.leftmost_subexpression(),
);
self.space(); self.space();
self.word(op.node.as_str()); self.word(op.node.as_str());
self.word_space("="); self.word_space("=");
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression()); self.print_expr_cond_paren(
rhs,
rhs.precedence() < prec,
fixup.subsequent_subexpression(),
);
} }
ast::ExprKind::Field(expr, ident) => { ast::ExprKind::Field(expr, ident) => {
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup); self.print_expr_cond_paren(
expr,
expr.precedence() < parser::PREC_UNAMBIGUOUS,
fixup,
);
self.word("."); self.word(".");
self.print_ident(*ident); self.print_ident(*ident);
} }
ast::ExprKind::Index(expr, index, _) => { ast::ExprKind::Index(expr, index, _) => {
self.print_expr_maybe_paren( self.print_expr_cond_paren(
expr, expr,
parser::PREC_UNAMBIGUOUS, expr.precedence() < parser::PREC_UNAMBIGUOUS,
fixup.leftmost_subexpression(), fixup.leftmost_subexpression(),
); );
self.word("["); self.word("[");
@ -590,14 +638,22 @@ impl<'a> State<'a> {
// a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.) // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
let fake_prec = AssocOp::LOr.precedence() as i8; let fake_prec = AssocOp::LOr.precedence() as i8;
if let Some(e) = start { if let Some(e) = start {
self.print_expr_maybe_paren(e, fake_prec, fixup.leftmost_subexpression()); self.print_expr_cond_paren(
e,
e.precedence() < fake_prec,
fixup.leftmost_subexpression(),
);
} }
match limits { match limits {
ast::RangeLimits::HalfOpen => self.word(".."), ast::RangeLimits::HalfOpen => self.word(".."),
ast::RangeLimits::Closed => self.word("..="), ast::RangeLimits::Closed => self.word("..="),
} }
if let Some(e) = end { if let Some(e) = end {
self.print_expr_maybe_paren(e, fake_prec, fixup.subsequent_subexpression()); self.print_expr_cond_paren(
e,
e.precedence() < fake_prec,
fixup.subsequent_subexpression(),
);
} }
} }
ast::ExprKind::Underscore => self.word("_"), ast::ExprKind::Underscore => self.word("_"),
@ -632,9 +688,9 @@ impl<'a> State<'a> {
self.word("return"); self.word("return");
if let Some(expr) = result { if let Some(expr) = result {
self.word(" "); self.word(" ");
self.print_expr_maybe_paren( self.print_expr_cond_paren(
expr, expr,
parser::PREC_JUMP, expr.precedence() < parser::PREC_JUMP,
fixup.subsequent_subexpression(), fixup.subsequent_subexpression(),
); );
} }
@ -645,9 +701,9 @@ impl<'a> State<'a> {
self.word("yeet"); self.word("yeet");
if let Some(expr) = result { if let Some(expr) = result {
self.word(" "); self.word(" ");
self.print_expr_maybe_paren( self.print_expr_cond_paren(
expr, expr,
parser::PREC_JUMP, expr.precedence() < parser::PREC_JUMP,
fixup.subsequent_subexpression(), fixup.subsequent_subexpression(),
); );
} }
@ -655,9 +711,9 @@ impl<'a> State<'a> {
ast::ExprKind::Become(result) => { ast::ExprKind::Become(result) => {
self.word("become"); self.word("become");
self.word(" "); self.word(" ");
self.print_expr_maybe_paren( self.print_expr_cond_paren(
result, result,
parser::PREC_JUMP, result.precedence() < parser::PREC_JUMP,
fixup.subsequent_subexpression(), fixup.subsequent_subexpression(),
); );
} }
@ -709,15 +765,15 @@ impl<'a> State<'a> {
if let Some(expr) = e { if let Some(expr) = e {
self.space(); self.space();
self.print_expr_maybe_paren( self.print_expr_cond_paren(
expr, expr,
parser::PREC_JUMP, expr.precedence() < parser::PREC_JUMP,
fixup.subsequent_subexpression(), fixup.subsequent_subexpression(),
); );
} }
} }
ast::ExprKind::Try(e) => { ast::ExprKind::Try(e) => {
self.print_expr_maybe_paren(e, parser::PREC_UNAMBIGUOUS, fixup); self.print_expr_cond_paren(e, e.precedence() < parser::PREC_UNAMBIGUOUS, fixup);
self.word("?") self.word("?")
} }
ast::ExprKind::TryBlock(blk) => { ast::ExprKind::TryBlock(blk) => {

View file

@ -1,5 +1,6 @@
use rustc_abi::Primitive::{Int, Pointer}; use rustc_abi::Primitive::{Int, Pointer};
use rustc_abi::{Align, FieldsShape, Size, TagEncoding, VariantIdx, Variants}; use rustc_abi::{Align, BackendRepr, FieldsShape, Size, TagEncoding, VariantIdx, Variants};
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::tcx::PlaceTy; use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout}; use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
@ -385,15 +386,22 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
if variant_index != untagged_variant { if variant_index != untagged_variant {
let niche = self.project_field(bx, tag_field); let niche = self.project_field(bx, tag_field);
let niche_llty = bx.cx().immediate_backend_type(niche.layout); let niche_llty = bx.cx().immediate_backend_type(niche.layout);
let BackendRepr::Scalar(scalar) = niche.layout.backend_repr else {
bug!("expected a scalar placeref for the niche");
};
// We are supposed to compute `niche_value.wrapping_add(niche_start)` wrapping
// around the `niche`'s type.
// The easiest way to do that is to do wrapping arithmetic on `u128` and then
// masking off any extra bits that occur because we did the arithmetic with too many bits.
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32(); let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
let niche_value = (niche_value as u128).wrapping_add(niche_start); let niche_value = (niche_value as u128).wrapping_add(niche_start);
// FIXME(eddyb): check the actual primitive type here. let niche_value = niche_value & niche.layout.size.unsigned_int_max();
let niche_llval = if niche_value == 0 {
// HACK(eddyb): using `c_null` as it works on all types. let niche_llval = bx.cx().scalar_to_backend(
bx.cx().const_null(niche_llty) Scalar::from_uint(niche_value, niche.layout.size),
} else { scalar,
bx.cx().const_uint_big(niche_llty, niche_value) niche_llty,
}; );
OperandValue::Immediate(niche_llval).store(bx, niche); OperandValue::Immediate(niche_llval).store(bx, niche);
} }
} }

View file

@ -1010,10 +1010,6 @@ impl<'a> State<'a> {
self.pclose() self.pclose()
} }
fn print_expr_maybe_paren(&mut self, expr: &hir::Expr<'_>, prec: i8) {
self.print_expr_cond_paren(expr, expr.precedence() < prec)
}
/// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
/// `if cond { ... }`. /// `if cond { ... }`.
fn print_expr_as_cond(&mut self, expr: &hir::Expr<'_>) { fn print_expr_as_cond(&mut self, expr: &hir::Expr<'_>) {
@ -1141,7 +1137,7 @@ impl<'a> State<'a> {
_ => parser::PREC_UNAMBIGUOUS, _ => parser::PREC_UNAMBIGUOUS,
}; };
self.print_expr_maybe_paren(func, prec); self.print_expr_cond_paren(func, func.precedence() < prec);
self.print_call_post(args) self.print_call_post(args)
} }
@ -1152,7 +1148,7 @@ impl<'a> State<'a> {
args: &[hir::Expr<'_>], args: &[hir::Expr<'_>],
) { ) {
let base_args = args; let base_args = args;
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS); self.print_expr_cond_paren(receiver, receiver.precedence() < parser::PREC_UNAMBIGUOUS);
self.word("."); self.word(".");
self.print_ident(segment.ident); self.print_ident(segment.ident);
@ -1188,15 +1184,15 @@ impl<'a> State<'a> {
_ => left_prec, _ => left_prec,
}; };
self.print_expr_maybe_paren(lhs, left_prec); self.print_expr_cond_paren(lhs, lhs.precedence() < left_prec);
self.space(); self.space();
self.word_space(op.node.as_str()); self.word_space(op.node.as_str());
self.print_expr_maybe_paren(rhs, right_prec) self.print_expr_cond_paren(rhs, rhs.precedence() < right_prec)
} }
fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr<'_>) { fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr<'_>) {
self.word(op.as_str()); self.word(op.as_str());
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX) self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_PREFIX)
} }
fn print_expr_addr_of( fn print_expr_addr_of(
@ -1213,7 +1209,7 @@ impl<'a> State<'a> {
self.print_mutability(mutability, true); self.print_mutability(mutability, true);
} }
} }
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX) self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_PREFIX)
} }
fn print_literal(&mut self, lit: &hir::Lit) { fn print_literal(&mut self, lit: &hir::Lit) {
@ -1352,7 +1348,7 @@ impl<'a> State<'a> {
} }
hir::ExprKind::Cast(expr, ty) => { hir::ExprKind::Cast(expr, ty) => {
let prec = AssocOp::As.precedence() as i8; let prec = AssocOp::As.precedence() as i8;
self.print_expr_maybe_paren(expr, prec); self.print_expr_cond_paren(expr, expr.precedence() < prec);
self.space(); self.space();
self.word_space("as"); self.word_space("as");
self.print_type(ty); self.print_type(ty);
@ -1454,26 +1450,26 @@ impl<'a> State<'a> {
} }
hir::ExprKind::Assign(lhs, rhs, _) => { hir::ExprKind::Assign(lhs, rhs, _) => {
let prec = AssocOp::Assign.precedence() as i8; let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1); self.print_expr_cond_paren(lhs, lhs.precedence() <= prec);
self.space(); self.space();
self.word_space("="); self.word_space("=");
self.print_expr_maybe_paren(rhs, prec); self.print_expr_cond_paren(rhs, rhs.precedence() < prec);
} }
hir::ExprKind::AssignOp(op, lhs, rhs) => { hir::ExprKind::AssignOp(op, lhs, rhs) => {
let prec = AssocOp::Assign.precedence() as i8; let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1); self.print_expr_cond_paren(lhs, lhs.precedence() <= prec);
self.space(); self.space();
self.word(op.node.as_str()); self.word(op.node.as_str());
self.word_space("="); self.word_space("=");
self.print_expr_maybe_paren(rhs, prec); self.print_expr_cond_paren(rhs, rhs.precedence() < prec);
} }
hir::ExprKind::Field(expr, ident) => { hir::ExprKind::Field(expr, ident) => {
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS); self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_UNAMBIGUOUS);
self.word("."); self.word(".");
self.print_ident(ident); self.print_ident(ident);
} }
hir::ExprKind::Index(expr, index, _) => { hir::ExprKind::Index(expr, index, _) => {
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS); self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_UNAMBIGUOUS);
self.word("["); self.word("[");
self.print_expr(index); self.print_expr(index);
self.word("]"); self.word("]");
@ -1487,7 +1483,7 @@ impl<'a> State<'a> {
} }
if let Some(expr) = opt_expr { if let Some(expr) = opt_expr {
self.space(); self.space();
self.print_expr_maybe_paren(expr, parser::PREC_JUMP); self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_JUMP);
} }
} }
hir::ExprKind::Continue(destination) => { hir::ExprKind::Continue(destination) => {
@ -1501,13 +1497,13 @@ impl<'a> State<'a> {
self.word("return"); self.word("return");
if let Some(expr) = result { if let Some(expr) = result {
self.word(" "); self.word(" ");
self.print_expr_maybe_paren(expr, parser::PREC_JUMP); self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_JUMP);
} }
} }
hir::ExprKind::Become(result) => { hir::ExprKind::Become(result) => {
self.word("become"); self.word("become");
self.word(" "); self.word(" ");
self.print_expr_maybe_paren(result, parser::PREC_JUMP); self.print_expr_cond_paren(result, result.precedence() < parser::PREC_JUMP);
} }
hir::ExprKind::InlineAsm(asm) => { hir::ExprKind::InlineAsm(asm) => {
self.word("asm!"); self.word("asm!");
@ -1532,7 +1528,7 @@ impl<'a> State<'a> {
} }
hir::ExprKind::Yield(expr, _) => { hir::ExprKind::Yield(expr, _) => {
self.word_space("yield"); self.word_space("yield");
self.print_expr_maybe_paren(expr, parser::PREC_JUMP); self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_JUMP);
} }
hir::ExprKind::Err(_) => { hir::ExprKind::Err(_) => {
self.popen(); self.popen();

View file

@ -9,7 +9,7 @@ pub(crate) fn target() -> Target {
description: Some("ARM64 Apple visionOS".into()), description: Some("ARM64 Apple visionOS".into()),
tier: Some(3), tier: Some(3),
host_tools: Some(false), host_tools: Some(false),
std: Some(false), std: Some(true),
}, },
pointer_width: 64, pointer_width: 64,
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32" data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"

View file

@ -9,7 +9,7 @@ pub(crate) fn target() -> Target {
description: Some("ARM64 Apple visionOS simulator".into()), description: Some("ARM64 Apple visionOS simulator".into()),
tier: Some(3), tier: Some(3),
host_tools: Some(false), host_tools: Some(false),
std: Some(false), std: Some(true),
}, },
pointer_width: 64, pointer_width: 64,
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32" data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"

View file

@ -135,9 +135,9 @@ dependencies = [
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.15.0" version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
dependencies = [ dependencies = [
"allocator-api2", "allocator-api2",
"compiler_builtins", "compiler_builtins",

View file

@ -203,10 +203,8 @@ fn rm_rf(path: &Path) {
do_op(path, "remove dir", |p| match fs::remove_dir(p) { do_op(path, "remove dir", |p| match fs::remove_dir(p) {
// Check for dir not empty on Windows // Check for dir not empty on Windows
// FIXME: Once `ErrorKind::DirectoryNotEmpty` is stabilized,
// match on `e.kind()` instead.
#[cfg(windows)] #[cfg(windows)]
Err(e) if e.raw_os_error() == Some(145) => Ok(()), Err(e) if e.kind() == ErrorKind::DirectoryNotEmpty => Ok(()),
r => r, r => r,
}); });
} }

View file

@ -27,7 +27,6 @@ fn main() {
std::env::set_current_dir(CRATE).unwrap(); std::env::set_current_dir(CRATE).unwrap();
let target_dir = path("target"); let target_dir = path("target");
let manifest_path = path("Cargo.toml");
// Debug // Debug
cargo() cargo()

View file

@ -0,0 +1,38 @@
//@ run-pass
//! Check that we can codegen setting and getting discriminants, including non-null niches,
//! for enums with a pointer-like ABI. This used to crash llvm.
#![feature(rustc_attrs)]
use std::{ptr, mem};
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_layout_scalar_valid_range_end(100)]
#[derive(Copy, Clone)]
struct PointerWithRange(#[allow(dead_code)] *const u8);
fn main() {
let val = unsafe { PointerWithRange(ptr::without_provenance(90)) };
let ptr = Some(val);
assert!(ptr.is_some());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert_eq!(raw, 90);
let ptr = Some(Some(val));
assert!(ptr.is_some());
assert!(ptr.unwrap().is_some());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert_eq!(raw, 90);
let ptr: Option<PointerWithRange> = None;
assert!(ptr.is_none());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert!(!(1..=100).contains(&raw));
let ptr: Option<Option<PointerWithRange>> = None;
assert!(ptr.is_none());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert!(!(1..=100).contains(&raw));
}

View file

@ -5,6 +5,7 @@
#![allow(dead_code)] #![allow(dead_code)]
#![feature(never_type)] #![feature(never_type)]
#![feature(pointer_is_aligned_to)] #![feature(pointer_is_aligned_to)]
#![feature(rustc_attrs)]
use std::mem::size_of; use std::mem::size_of;
use std::num::NonZero; use std::num::NonZero;
@ -237,6 +238,10 @@ struct VecDummy {
len: usize, len: usize,
} }
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_layout_scalar_valid_range_end(100)]
struct PointerWithRange(#[allow(dead_code)] *const u8);
pub fn main() { pub fn main() {
assert_eq!(size_of::<u8>(), 1 as usize); assert_eq!(size_of::<u8>(), 1 as usize);
assert_eq!(size_of::<u32>(), 4 as usize); assert_eq!(size_of::<u32>(), 4 as usize);
@ -354,4 +359,6 @@ pub fn main() {
assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr()); assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr());
assert_eq!(size_of::<Option<PointerWithRange>>(), size_of::<PointerWithRange>());
assert_eq!(size_of::<Option<Option<PointerWithRange>>>(), size_of::<PointerWithRange>());
} }