Skip to content

Commit ddabe07

Browse files
committed
Auto merge of rust-lang#92518 - matthiaskrgr:rollup-fl8z4e7, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#90102 (Remove `NullOp::Box`) - rust-lang#92011 (Use field span in `rustc_macros` when emitting decode call) - rust-lang#92402 (Suggest while let x = y when encountering while x = y) - rust-lang#92409 (Couple of libtest cleanups) - rust-lang#92418 (Fix spacing in pretty printed PatKind::Struct with no fields) - rust-lang#92444 (Consolidate Result's and Option's methods into fewer impl blocks) Failed merges: - rust-lang#92483 (Stabilize `result_cloned` and `result_copied`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b5efe57 + 13e2840 commit ddabe07

File tree

31 files changed

+628
-665
lines changed

31 files changed

+628
-665
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,11 @@ impl<'a> State<'a> {
24612461
self.print_path(path, true, 0);
24622462
}
24632463
self.nbsp();
2464-
self.word_space("{");
2464+
self.word("{");
2465+
let empty = fields.is_empty() && !etc;
2466+
if !empty {
2467+
self.space();
2468+
}
24652469
self.commasep_cmnt(
24662470
Consistent,
24672471
&fields,
@@ -2482,7 +2486,9 @@ impl<'a> State<'a> {
24822486
}
24832487
self.word("..");
24842488
}
2485-
self.space();
2489+
if !empty {
2490+
self.space();
2491+
}
24862492
self.word("}");
24872493
}
24882494
PatKind::Tuple(ref elts) => {

compiler/rustc_borrowck/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1394,10 +1394,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13941394

13951395
Rvalue::NullaryOp(_op, _ty) => {
13961396
// nullary ops take no dynamic input; no borrowck effect.
1397-
//
1398-
// FIXME: is above actually true? Do we want to track
1399-
// the fact that uninitialized data can be created via
1400-
// `NullOp::Box`?
14011397
}
14021398

14031399
Rvalue::Aggregate(ref aggregate_kind, ref operands) => {

compiler/rustc_codegen_cranelift/src/base.rs

-25
Original file line numberDiff line numberDiff line change
@@ -715,30 +715,6 @@ fn codegen_stmt<'tcx>(
715715
let operand = operand.load_scalar(fx);
716716
lval.write_cvalue(fx, CValue::by_val(operand, box_layout));
717717
}
718-
Rvalue::NullaryOp(NullOp::Box, content_ty) => {
719-
let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
720-
let content_ty = fx.monomorphize(content_ty);
721-
let layout = fx.layout_of(content_ty);
722-
let llsize = fx.bcx.ins().iconst(usize_type, layout.size.bytes() as i64);
723-
let llalign = fx.bcx.ins().iconst(usize_type, layout.align.abi.bytes() as i64);
724-
let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
725-
726-
// Allocate space:
727-
let def_id =
728-
match fx.tcx.lang_items().require(rustc_hir::LangItem::ExchangeMalloc) {
729-
Ok(id) => id,
730-
Err(s) => {
731-
fx.tcx
732-
.sess
733-
.fatal(&format!("allocation of `{}` {}", box_layout.ty, s));
734-
}
735-
};
736-
let instance = ty::Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx);
737-
let func_ref = fx.get_function_ref(instance);
738-
let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]);
739-
let ptr = fx.bcx.inst_results(call)[0];
740-
lval.write_cvalue(fx, CValue::by_val(ptr, box_layout));
741-
}
742718
Rvalue::NullaryOp(null_op, ty) => {
743719
assert!(
744720
lval.layout()
@@ -749,7 +725,6 @@ fn codegen_stmt<'tcx>(
749725
let val = match null_op {
750726
NullOp::SizeOf => layout.size.bytes(),
751727
NullOp::AlignOf => layout.align.abi.bytes(),
752-
NullOp::Box => unreachable!(),
753728
};
754729
let val = CValue::const_val(fx, fx.layout_of(fx.tcx.types.usize), val.into());
755730
lval.write_cvalue(fx, val);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::traits::*;
88
use crate::MemFlags;
99

1010
use rustc_apfloat::{ieee, Float, Round, Status};
11-
use rustc_hir::lang_items::LangItem;
1211
use rustc_middle::mir;
1312
use rustc_middle::ty::cast::{CastTy, IntTy};
1413
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
@@ -486,39 +485,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
486485
)
487486
}
488487

489-
mir::Rvalue::NullaryOp(mir::NullOp::Box, content_ty) => {
490-
let content_ty = self.monomorphize(content_ty);
491-
let content_layout = bx.cx().layout_of(content_ty);
492-
let llsize = bx.cx().const_usize(content_layout.size.bytes());
493-
let llalign = bx.cx().const_usize(content_layout.align.abi.bytes());
494-
let box_layout = bx.cx().layout_of(bx.tcx().mk_box(content_ty));
495-
let llty_ptr = bx.cx().backend_type(box_layout);
496-
497-
// Allocate space:
498-
let def_id = match bx.tcx().lang_items().require(LangItem::ExchangeMalloc) {
499-
Ok(id) => id,
500-
Err(s) => {
501-
bx.cx().sess().fatal(&format!("allocation of `{}` {}", box_layout.ty, s));
502-
}
503-
};
504-
let instance = ty::Instance::mono(bx.tcx(), def_id);
505-
let r = bx.cx().get_fn_addr(instance);
506-
let ty = bx.type_func(&[bx.type_isize(), bx.type_isize()], bx.type_i8p());
507-
let call = bx.call(ty, r, &[llsize, llalign], None);
508-
let val = bx.pointercast(call, llty_ptr);
509-
510-
let operand = OperandRef { val: OperandValue::Immediate(val), layout: box_layout };
511-
(bx, operand)
512-
}
513-
514488
mir::Rvalue::NullaryOp(null_op, ty) => {
515489
let ty = self.monomorphize(ty);
516490
assert!(bx.cx().type_is_sized(ty));
517491
let layout = bx.cx().layout_of(ty);
518492
let val = match null_op {
519493
mir::NullOp::SizeOf => layout.size.bytes(),
520494
mir::NullOp::AlignOf => layout.align.abi.bytes(),
521-
mir::NullOp::Box => unreachable!(),
522495
};
523496
let val = bx.cx().const_usize(val);
524497
let tcx = self.cx.tcx();

compiler/rustc_const_eval/src/const_eval/machine.rs

-7
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
398398
Err(ConstEvalErrKind::NeedsRfc("pointer arithmetic or comparison".to_string()).into())
399399
}
400400

401-
fn box_alloc(
402-
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
403-
_dest: &PlaceTy<'tcx>,
404-
) -> InterpResult<'tcx> {
405-
Err(ConstEvalErrKind::NeedsRfc("heap allocations via `box` keyword".to_string()).into())
406-
}
407-
408401
fn before_terminator(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
409402
// The step limit has already been hit in a previous call to `before_terminator`.
410403
if ecx.machine.steps_remaining == 0 {

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub enum StackPopCleanup {
156156
/// `ret` stores the block we jump to on a normal return, while `unwind`
157157
/// stores the block used for cleanup during unwinding.
158158
Goto { ret: Option<mir::BasicBlock>, unwind: StackPopUnwind },
159-
/// Just do nothing: Used by Main and for the `box_alloc` hook in miri.
159+
/// Just do nothing: Used by Main and for TLS hooks in miri.
160160
/// `cleanup` says whether locals are deallocated. Static computation
161161
/// wants them leaked to intern what they need (and just throw away
162162
/// the entire `ecx` when it is done).

compiler/rustc_const_eval/src/interpret/machine.rs

-6
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
212212
right: &ImmTy<'tcx, Self::PointerTag>,
213213
) -> InterpResult<'tcx, (Scalar<Self::PointerTag>, bool, Ty<'tcx>)>;
214214

215-
/// Heap allocations via the `box` keyword.
216-
fn box_alloc(
217-
ecx: &mut InterpCx<'mir, 'tcx, Self>,
218-
dest: &PlaceTy<'tcx, Self::PointerTag>,
219-
) -> InterpResult<'tcx>;
220-
221215
/// Called to read the specified `local` from the `frame`.
222216
/// Since reading a ZST is not actually accessing memory or locals, this is never invoked
223217
/// for ZST reads.

compiler/rustc_const_eval/src/interpret/step.rs

-5
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
271271
self.write_immediate(place.to_ref(self), &dest)?;
272272
}
273273

274-
NullaryOp(mir::NullOp::Box, _) => {
275-
M::box_alloc(self, &dest)?;
276-
}
277-
278274
NullaryOp(null_op, ty) => {
279275
let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty)?;
280276
let layout = self.layout_of(ty)?;
@@ -289,7 +285,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
289285
let val = match null_op {
290286
mir::NullOp::SizeOf => layout.size.bytes(),
291287
mir::NullOp::AlignOf => layout.align.abi.bytes(),
292-
mir::NullOp::Box => unreachable!(),
293288
};
294289
self.write_scalar(Scalar::from_machine_usize(val, self), &dest)?;
295290
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
632632
}
633633

634634
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => {}
635-
Rvalue::NullaryOp(NullOp::Box, _) => self.check_op(ops::HeapAllocation),
636635
Rvalue::ShallowInitBox(_, _) => {}
637636

638637
Rvalue::UnaryOp(_, ref operand) => {

compiler/rustc_const_eval/src/transform/promote_consts.rs

-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ impl<'tcx> Validator<'_, 'tcx> {
508508
}
509509

510510
Rvalue::NullaryOp(op, _) => match op {
511-
NullOp::Box => return Err(Unpromotable),
512511
NullOp::SizeOf => {}
513512
NullOp::AlignOf => {}
514513
},

compiler/rustc_hir_pretty/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,11 @@ impl<'a> State<'a> {
18741874
PatKind::Struct(ref qpath, ref fields, etc) => {
18751875
self.print_qpath(qpath, true);
18761876
self.nbsp();
1877-
self.word_space("{");
1877+
self.word("{");
1878+
let empty = fields.is_empty() && !etc;
1879+
if !empty {
1880+
self.space();
1881+
}
18781882
self.commasep_cmnt(
18791883
Consistent,
18801884
&fields,
@@ -1895,7 +1899,9 @@ impl<'a> State<'a> {
18951899
}
18961900
self.word("..");
18971901
}
1898-
self.space();
1902+
if !empty {
1903+
self.space();
1904+
}
18991905
self.word("}");
19001906
}
19011907
PatKind::Or(ref pats) => {

compiler/rustc_macros/src/serialize.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use proc_macro2::TokenStream;
2-
use quote::quote;
2+
use quote::{quote, quote_spanned};
33
use syn::parse_quote;
4+
use syn::spanned::Spanned;
45

56
pub fn type_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
67
let decoder_ty = quote! { __D };
@@ -104,27 +105,30 @@ fn decodable_body(
104105
}
105106

106107
fn decode_field(field: &syn::Field, index: usize, is_struct: bool) -> proc_macro2::TokenStream {
108+
let field_span = field.ident.as_ref().map_or(field.ty.span(), |ident| ident.span());
109+
107110
let decode_inner_method = if let syn::Type::Reference(_) = field.ty {
108111
quote! { ::rustc_middle::ty::codec::RefDecodable::decode }
109112
} else {
110113
quote! { ::rustc_serialize::Decodable::decode }
111114
};
112115
let (decode_method, opt_field_name) = if is_struct {
113116
let field_name = field.ident.as_ref().map_or_else(|| index.to_string(), |i| i.to_string());
114-
(
115-
proc_macro2::Ident::new("read_struct_field", proc_macro2::Span::call_site()),
116-
quote! { #field_name, },
117-
)
117+
(proc_macro2::Ident::new("read_struct_field", field_span), quote! { #field_name, })
118118
} else {
119-
(
120-
proc_macro2::Ident::new("read_enum_variant_arg", proc_macro2::Span::call_site()),
121-
quote! {},
122-
)
119+
(proc_macro2::Ident::new("read_enum_variant_arg", field_span), quote! {})
120+
};
121+
122+
let __decoder = quote! { __decoder };
123+
// Use the span of the field for the method call, so
124+
// that backtraces will point to the field.
125+
let decode_call = quote_spanned! {field_span=>
126+
::rustc_serialize::Decoder::#decode_method(
127+
#__decoder, #opt_field_name #decode_inner_method)
123128
};
124129

125130
quote! {
126-
match ::rustc_serialize::Decoder::#decode_method(
127-
__decoder, #opt_field_name #decode_inner_method) {
131+
match #decode_call {
128132
::std::result::Result::Ok(__res) => __res,
129133
::std::result::Result::Err(__err) => return ::std::result::Result::Err(__err),
130134
}

compiler/rustc_middle/src/mir/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2336,8 +2336,6 @@ pub enum NullOp {
23362336
SizeOf,
23372337
/// Returns the minimum alignment of a type
23382338
AlignOf,
2339-
/// Creates a new uninitialized box for a value of that type
2340-
Box,
23412339
}
23422340

23432341
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]

compiler/rustc_middle/src/mir/tcx.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ impl<'tcx> Rvalue<'tcx> {
195195
}
196196
Rvalue::UnaryOp(UnOp::Not | UnOp::Neg, ref operand) => operand.ty(local_decls, tcx),
197197
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
198-
Rvalue::NullaryOp(NullOp::Box, t) => tcx.mk_box(t),
199198
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => tcx.types.usize,
200199
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
201200
AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
@@ -215,9 +214,7 @@ impl<'tcx> Rvalue<'tcx> {
215214
/// whether its only shallowly initialized (`Rvalue::Box`).
216215
pub fn initialization_state(&self) -> RvalueInitializationState {
217216
match *self {
218-
Rvalue::NullaryOp(NullOp::Box, _) | Rvalue::ShallowInitBox(_, _) => {
219-
RvalueInitializationState::Shallow
220-
}
217+
Rvalue::ShallowInitBox(_, _) => RvalueInitializationState::Shallow,
221218
_ => RvalueInitializationState::Deep,
222219
}
223220
}

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
343343
| Rvalue::AddressOf(..)
344344
| Rvalue::Discriminant(..)
345345
| Rvalue::Len(..)
346-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _)
347-
| Rvalue::NullaryOp(NullOp::Box, _) => {
348-
// This returns an rvalue with uninitialized contents. We can't
349-
// move out of it here because it is an rvalue - assignments always
350-
// completely initialize their place.
351-
//
352-
// However, this does not matter - MIR building is careful to
353-
// only emit a shallow free for the partially-initialized
354-
// temporary.
355-
//
356-
// In any case, if we want to fix this, we have to register a
357-
// special move and change the `statement_effect` functions.
358-
}
346+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => {}
359347
}
360348
}
361349

compiler/rustc_mir_transform/src/const_prop.rs

-7
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
239239
throw_machine_stop_str!("pointer arithmetic or comparisons aren't supported in ConstProp")
240240
}
241241

242-
fn box_alloc(
243-
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
244-
_dest: &PlaceTy<'tcx>,
245-
) -> InterpResult<'tcx> {
246-
throw_machine_stop_str!("can't const prop heap allocations")
247-
}
248-
249242
fn access_local(
250243
_ecx: &InterpCx<'mir, 'tcx, Self>,
251244
frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,

compiler/rustc_monomorphize/src/collector.rs

-9
Original file line numberDiff line numberDiff line change
@@ -688,15 +688,6 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
688688
_ => bug!(),
689689
}
690690
}
691-
mir::Rvalue::NullaryOp(mir::NullOp::Box, _) => {
692-
let tcx = self.tcx;
693-
let exchange_malloc_fn_def_id =
694-
tcx.require_lang_item(LangItem::ExchangeMalloc, None);
695-
let instance = Instance::mono(tcx, exchange_malloc_fn_def_id);
696-
if should_codegen_locally(tcx, &instance) {
697-
self.output.push(create_fn_mono_item(self.tcx, instance, span));
698-
}
699-
}
700691
mir::Rvalue::ThreadLocalRef(def_id) => {
701692
assert!(self.tcx.is_thread_local_static(def_id));
702693
let instance = Instance::mono(self.tcx, def_id);

compiler/rustc_resolve/src/late.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23792379
ExprKind::While(ref cond, ref block, label) => {
23802380
self.with_resolved_label(label, expr.id, |this| {
23812381
this.with_rib(ValueNS, NormalRibKind, |this| {
2382+
let old = this.diagnostic_metadata.in_if_condition.replace(cond);
23822383
this.visit_expr(cond);
2384+
this.diagnostic_metadata.in_if_condition = old;
23832385
this.visit_block(block);
23842386
})
23852387
});

0 commit comments

Comments
 (0)