Skip to content

Commit 2a979f8

Browse files
committed
Auto merge of #2794 - RalfJung:rustup, r=RalfJung
Rustup
2 parents 9669e57 + 4e0cb32 commit 2a979f8

File tree

209 files changed

+3855
-1975
lines changed

Some content is hidden

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

209 files changed

+3855
-1975
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,6 +4180,7 @@ dependencies = [
41804180
"rustc_hir_analysis",
41814181
"rustc_hir_typeck",
41824182
"rustc_incremental",
4183+
"rustc_index",
41834184
"rustc_lint",
41844185
"rustc_macros",
41854186
"rustc_metadata",

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
1313
use rustc_hir::PredicateOrigin;
1414
use rustc_index::vec::{Idx, IndexVec};
1515
use rustc_middle::ty::{DefIdTree, ResolverAstLowering, TyCtxt};
16-
use rustc_span::lev_distance::find_best_match_for_name;
16+
use rustc_span::edit_distance::find_best_match_for_name;
1717
use rustc_span::source_map::DesugaringKind;
1818
use rustc_span::symbol::{kw, sym, Ident};
1919
use rustc_span::{Span, Symbol};

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
534534
return PlaceTy::from_ty(self.tcx().ty_error());
535535
}
536536
}
537-
place_ty = self.sanitize_projection(place_ty, elem, place, location);
537+
place_ty = self.sanitize_projection(place_ty, elem, place, location, context);
538538
}
539539

540540
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
@@ -630,12 +630,14 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
630630
}
631631
}
632632

633+
#[instrument(skip(self), level = "debug")]
633634
fn sanitize_projection(
634635
&mut self,
635636
base: PlaceTy<'tcx>,
636637
pi: PlaceElem<'tcx>,
637638
place: &Place<'tcx>,
638639
location: Location,
640+
context: PlaceContext,
639641
) -> PlaceTy<'tcx> {
640642
debug!("sanitize_projection: {:?} {:?} {:?}", base, pi, place);
641643
let tcx = self.tcx();
@@ -713,8 +715,11 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
713715
match self.field_ty(place, base, field, location) {
714716
Ok(ty) => {
715717
let ty = self.cx.normalize(ty, location);
716-
if let Err(terr) = self.cx.eq_types(
718+
debug!(?fty, ?ty);
719+
720+
if let Err(terr) = self.cx.relate_types(
717721
ty,
722+
self.get_ambient_variance(context),
718723
fty,
719724
location.to_locations(),
720725
ConstraintCategory::Boring,
@@ -743,9 +748,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
743748
let ty = self.sanitize_type(place, ty);
744749
let ty = self.cx.normalize(ty, location);
745750
self.cx
746-
.eq_types(
747-
base.ty,
751+
.relate_types(
748752
ty,
753+
self.get_ambient_variance(context),
754+
base.ty,
749755
location.to_locations(),
750756
ConstraintCategory::TypeAnnotation,
751757
)
@@ -760,6 +766,21 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
760766
self.tcx().ty_error()
761767
}
762768

769+
fn get_ambient_variance(&self, context: PlaceContext) -> ty::Variance {
770+
use rustc_middle::mir::visit::NonMutatingUseContext::*;
771+
use rustc_middle::mir::visit::NonUseContext::*;
772+
773+
match context {
774+
PlaceContext::MutatingUse(_) => ty::Invariant,
775+
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
776+
PlaceContext::NonMutatingUse(
777+
Inspect | Copy | Move | SharedBorrow | ShallowBorrow | UniqueBorrow | AddressOf
778+
| Projection,
779+
) => ty::Covariant,
780+
PlaceContext::NonUse(AscribeUserTy) => ty::Covariant,
781+
}
782+
}
783+
763784
fn field_ty(
764785
&mut self,
765786
parent: &dyn fmt::Debug,

compiler/rustc_codegen_ssa/src/back/metadata.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_target::spec::{RelocModel, Target};
3333
/// <dt>dylib</dt>
3434
/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
3535
/// </dl>
36+
#[derive(Debug)]
3637
pub struct DefaultMetadataLoader;
3738

3839
fn load_metadata_with(

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
312312
}
313313
}
314314

315+
/// `src` is a *pointer to* a `source_ty`, and in `dest` we should store a pointer to th same
316+
/// data at type `cast_ty`.
315317
fn unsize_into_ptr(
316318
&mut self,
317319
src: &OpTy<'tcx, M::Provenance>,
@@ -335,7 +337,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
335337
);
336338
self.write_immediate(val, dest)
337339
}
338-
(ty::Dynamic(data_a, ..), ty::Dynamic(data_b, ..)) => {
340+
(ty::Dynamic(data_a, _, ty::Dyn), ty::Dynamic(data_b, _, ty::Dyn)) => {
339341
let val = self.read_immediate(src)?;
340342
if data_a.principal() == data_b.principal() {
341343
// A NOP cast that doesn't actually change anything, should be allowed even with mismatching vtables.
@@ -359,7 +361,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
359361
}
360362

361363
_ => {
362-
span_bug!(self.cur_span(), "invalid unsizing {:?} -> {:?}", src.layout.ty, cast_ty)
364+
span_bug!(
365+
self.cur_span(),
366+
"invalid pointer unsizing {:?} -> {:?}",
367+
src.layout.ty,
368+
cast_ty
369+
)
363370
}
364371
}
365372
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
632632
}
633633
Ok(Some((size, align)))
634634
}
635-
ty::Dynamic(..) => {
635+
ty::Dynamic(_, _, ty::Dyn) => {
636636
let vtable = metadata.unwrap_meta().to_pointer(self)?;
637637
// Read size and align from vtable (already checks size).
638638
Ok(Some(self.get_vtable_size_and_align(vtable)?))

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
242242
let mplace = self.ecx.ref_to_mplace(&value)?;
243243
assert_eq!(mplace.layout.ty, referenced_ty);
244244
// Handle trait object vtables.
245-
if let ty::Dynamic(..) =
245+
if let ty::Dynamic(_, _, ty::Dyn) =
246246
tcx.struct_tail_erasing_lifetimes(referenced_ty, self.ecx.param_env).kind()
247247
{
248248
let ptr = mplace.meta.unwrap_meta().to_pointer(&tcx)?;

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,22 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
255255
}
256256
}
257257

258-
pub fn offset_with_meta(
258+
/// Replace the layout of this operand. There's basically no sanity check that this makes sense,
259+
/// you better know what you are doing! If this is an immediate, applying the wrong layout can
260+
/// not just lead to invalid data, it can actually *shift the data around* since the offsets of
261+
/// a ScalarPair are entirely determined by the layout, not the data.
262+
pub fn transmute(&self, layout: TyAndLayout<'tcx>) -> Self {
263+
assert_eq!(
264+
self.layout.size, layout.size,
265+
"transmuting with a size change, that doesn't seem right"
266+
);
267+
OpTy { layout, ..*self }
268+
}
269+
270+
/// Offset the operand in memory (if possible) and change its metadata.
271+
///
272+
/// This can go wrong very easily if you give the wrong layout for the new place!
273+
pub(super) fn offset_with_meta(
259274
&self,
260275
offset: Size,
261276
meta: MemPlaceMeta<Prov>,
@@ -276,6 +291,9 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
276291
}
277292
}
278293

294+
/// Offset the operand in memory (if possible).
295+
///
296+
/// This can go wrong very easily if you give the wrong layout for the new place!
279297
pub fn offset(
280298
&self,
281299
offset: Size,

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub enum MemPlaceMeta<Prov: Provenance = AllocId> {
2626
}
2727

2828
impl<Prov: Provenance> MemPlaceMeta<Prov> {
29+
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
2930
pub fn unwrap_meta(self) -> Scalar<Prov> {
3031
match self {
3132
Self::Meta(s) => s,
@@ -147,12 +148,16 @@ impl<Prov: Provenance> MemPlace<Prov> {
147148
}
148149

149150
#[inline]
150-
pub fn offset_with_meta<'tcx>(
151+
pub(super) fn offset_with_meta<'tcx>(
151152
self,
152153
offset: Size,
153154
meta: MemPlaceMeta<Prov>,
154155
cx: &impl HasDataLayout,
155156
) -> InterpResult<'tcx, Self> {
157+
debug_assert!(
158+
!meta.has_meta() || self.meta.has_meta(),
159+
"cannot use `offset_with_meta` to add metadata to a place"
160+
);
156161
Ok(MemPlace { ptr: self.ptr.offset(offset, cx)?, meta })
157162
}
158163
}
@@ -182,8 +187,11 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
182187
MPlaceTy { mplace: MemPlace { ptr, meta: MemPlaceMeta::None }, layout, align }
183188
}
184189

190+
/// Offset the place in memory and change its metadata.
191+
///
192+
/// This can go wrong very easily if you give the wrong layout for the new place!
185193
#[inline]
186-
pub fn offset_with_meta(
194+
pub(crate) fn offset_with_meta(
187195
&self,
188196
offset: Size,
189197
meta: MemPlaceMeta<Prov>,
@@ -197,6 +205,9 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
197205
})
198206
}
199207

208+
/// Offset the place in memory.
209+
///
210+
/// This can go wrong very easily if you give the wrong layout for the new place!
200211
pub fn offset(
201212
&self,
202213
offset: Size,
@@ -241,14 +252,6 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
241252
}
242253
}
243254
}
244-
245-
#[inline]
246-
pub(super) fn vtable(&self) -> Scalar<Prov> {
247-
match self.layout.ty.kind() {
248-
ty::Dynamic(..) => self.mplace.meta.unwrap_meta(),
249-
_ => bug!("vtable not supported on type {:?}", self.layout.ty),
250-
}
251-
}
252255
}
253256

254257
// These are defined here because they produce a place.
@@ -266,7 +269,12 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
266269
#[inline(always)]
267270
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
268271
pub fn assert_mem_place(&self) -> MPlaceTy<'tcx, Prov> {
269-
self.as_mplace_or_imm().left().unwrap()
272+
self.as_mplace_or_imm().left().unwrap_or_else(|| {
273+
bug!(
274+
"OpTy of type {} was immediate when it was expected to be an MPlace",
275+
self.layout.ty
276+
)
277+
})
270278
}
271279
}
272280

@@ -283,7 +291,12 @@ impl<'tcx, Prov: Provenance> PlaceTy<'tcx, Prov> {
283291
#[inline(always)]
284292
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
285293
pub fn assert_mem_place(&self) -> MPlaceTy<'tcx, Prov> {
286-
self.as_mplace_or_local().left().unwrap()
294+
self.as_mplace_or_local().left().unwrap_or_else(|| {
295+
bug!(
296+
"PlaceTy of type {} was a local when it was expected to be an MPlace",
297+
self.layout.ty
298+
)
299+
})
287300
}
288301
}
289302

@@ -807,11 +820,16 @@ where
807820
}
808821

809822
/// Turn a place with a `dyn Trait` type into a place with the actual dynamic type.
823+
/// Aso returns the vtable.
810824
pub(super) fn unpack_dyn_trait(
811825
&self,
812826
mplace: &MPlaceTy<'tcx, M::Provenance>,
813-
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
814-
let vtable = mplace.vtable().to_pointer(self)?; // also sanity checks the type
827+
) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::Provenance>, Pointer<Option<M::Provenance>>)> {
828+
assert!(
829+
matches!(mplace.layout.ty.kind(), ty::Dynamic(_, _, ty::Dyn)),
830+
"`unpack_dyn_trait` only makes sense on `dyn*` types"
831+
);
832+
let vtable = mplace.meta.unwrap_meta().to_pointer(self)?;
815833
let (ty, _) = self.get_ptr_vtable(vtable)?;
816834
let layout = self.layout_of(ty)?;
817835

@@ -820,7 +838,26 @@ where
820838
layout,
821839
align: layout.align.abi,
822840
};
823-
Ok(mplace)
841+
Ok((mplace, vtable))
842+
}
843+
844+
/// Turn an operand with a `dyn* Trait` type into an operand with the actual dynamic type.
845+
/// Aso returns the vtable.
846+
pub(super) fn unpack_dyn_star(
847+
&self,
848+
op: &OpTy<'tcx, M::Provenance>,
849+
) -> InterpResult<'tcx, (OpTy<'tcx, M::Provenance>, Pointer<Option<M::Provenance>>)> {
850+
assert!(
851+
matches!(op.layout.ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
852+
"`unpack_dyn_star` only makes sense on `dyn*` types"
853+
);
854+
let data = self.operand_field(&op, 0)?;
855+
let vtable = self.operand_field(&op, 1)?;
856+
let vtable = self.read_pointer(&vtable)?;
857+
let (ty, _) = self.get_ptr_vtable(vtable)?;
858+
let layout = self.layout_of(ty)?;
859+
let data = data.transmute(layout);
860+
Ok((data, vtable))
824861
}
825862
}
826863

0 commit comments

Comments
 (0)