From b7266c6008ea2497da20d92d0c49ccc16bd8a699 Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Mon, 24 Dec 2018 17:52:50 +0100 Subject: [PATCH 01/16] Move pointee_info_at to TyLayoutMethods. The original implementation is still present at librustc_codegen_llvm/abi.rs, should be removed later to prevent code duplication. --- src/librustc/ty/layout.rs | 127 +++++++++++++++++++++++++++++++++ src/librustc_target/abi/mod.rs | 27 +++++++ 2 files changed, 154 insertions(+) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 21e4d8b07a1..a67aa0e41b3 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -12,6 +12,7 @@ use std::iter; use std::mem; use std::ops::Bound; +use hir; use crate::ich::StableHashingContext; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, @@ -1545,6 +1546,7 @@ impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> { pub trait MaybeResult { fn from_ok(x: T) -> Self; fn map_same T>(self, f: F) -> Self; + fn ok(self) -> Option; } impl MaybeResult for T { @@ -1554,6 +1556,9 @@ impl MaybeResult for T { fn map_same T>(self, f: F) -> Self { f(self) } + fn ok(self) -> Option { + Some(self) + } } impl MaybeResult for Result { @@ -1563,6 +1568,9 @@ impl MaybeResult for Result { fn map_same T>(self, f: F) -> Self { self.map(f) } + fn ok(self) -> Option { + self.ok() + } } pub type TyLayout<'tcx> = ::rustc_target::abi::TyLayout<'tcx, Ty<'tcx>>; @@ -1824,6 +1832,125 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } }) } + + fn pointee_info_at(this: TyLayout<'tcx>, cx: &C, offset: Size + ) -> Option { + let mut result = None; + match this.ty.sty { + ty::RawPtr(mt) if offset.bytes() == 0 => { + result = cx.layout_of(mt.ty).ok() + .map(|layout| PointeeInfo { + size: layout.size, + align: layout.align.abi, + safe: None, + }); + } + + ty::Ref(_, ty, mt) if offset.bytes() == 0 => { + let tcx = cx.tcx(); + let is_freeze = ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP); + let kind = match mt { + hir::MutImmutable => if is_freeze { + PointerKind::Frozen + } else { + PointerKind::Shared + }, + hir::MutMutable => { + // Previously we would only emit noalias annotations for LLVM >= 6 or in + // panic=abort mode. That was deemed right, as prior versions had many bugs + // in conjunction with unwinding, but later versions didn’t seem to have + // said issues. See issue #31681. + // + // Alas, later on we encountered a case where noalias would generate wrong + // code altogether even with recent versions of LLVM in *safe* code with no + // unwinding involved. See #54462. + // + // For now, do not enable mutable_noalias by default at all, while the + // issue is being figured out. + let mutable_noalias = tcx.sess.opts.debugging_opts.mutable_noalias + .unwrap_or(false); + if mutable_noalias { + PointerKind::UniqueBorrowed + } else { + PointerKind::Shared + } + } + }; + + result = cx.layout_of(ty).ok() + .map(|layout| PointeeInfo { + size: layout.size, + align: layout.align.abi, + safe: Some(kind), + }); + } + + _ => { + let mut data_variant = match this.variants { + Variants::NicheFilling { dataful_variant, .. } => { + // Only the niche itthis is always initialized, + // so only check for a pointer at its offset. + // + // If the niche is a pointer, it's either valid + // (according to its type), or null (which the + // niche field's scalar validity range encodes). + // This allows using `dereferenceable_or_null` + // for e.g., `Option<&T>`, and this will continue + // to work as long as we don't start using more + // niches than just null (e.g., the first page + // of the address space, or unaligned pointers). + if this.fields.offset(0) == offset { + Some(this.for_variant(cx, dataful_variant)) + } else { + None + } + } + _ => Some(this) + }; + + if let Some(variant) = data_variant { + // We're not interested in any unions. + if let FieldPlacement::Union(_) = variant.fields { + data_variant = None; + } + } + + if let Some(variant) = data_variant { + let ptr_end = offset + Pointer.size(cx); + for i in 0..variant.fields.count() { + let field_start = variant.fields.offset(i); + if field_start <= offset { + let field = variant.field(cx, i); + result = field.ok() + .and_then(|field| { + if ptr_end <= field_start + field.size { + // We found the right field, look inside it. + Self::pointee_info_at(field, cx, offset - field_start) + } else { + None + } + }); + if result.is_some() { + break; + } + } + } + } + + // FIXME(eddyb) This should be for `ptr::Unique`, not `Box`. + if let Some(ref mut pointee) = result { + if let ty::Adt(def, _) = this.ty.sty { + if def.is_box() && offset.bytes() == 0 { + pointee.safe = Some(PointerKind::UniqueOwned); + } + } + } + } + } + + result + } + } struct Niche { diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 59eda97a2f9..74257cb3f64 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -910,6 +910,28 @@ pub trait LayoutOf { fn layout_of(&self, ty: Self::Ty) -> Self::TyLayout; } +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum PointerKind { + /// Most general case, we know no restrictions to tell LLVM. + Shared, + + /// `&T` where `T` contains no `UnsafeCell`, is `noalias` and `readonly`. + Frozen, + + /// `&mut T`, when we know `noalias` is safe for LLVM. + UniqueBorrowed, + + /// `Box`, unlike `UniqueBorrowed`, it also has `noalias` on returns. + UniqueOwned +} + +#[derive(Copy, Clone)] +pub struct PointeeInfo { + pub size: Size, + pub align: Align, + pub safe: Option, +} + pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { fn for_variant( this: TyLayout<'a, Self>, @@ -917,6 +939,11 @@ pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { variant_index: VariantIdx, ) -> TyLayout<'a, Self>; fn field(this: TyLayout<'a, Self>, cx: &C, i: usize) -> C::TyLayout; + fn pointee_info_at( + this: TyLayout<'a, Self>, + cx: &C, + offset: Size + ) -> Option; } impl<'a, Ty> TyLayout<'a, Ty> { From e395026aba4d0739ec06264dabb97a614ec8d0dc Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 28 Dec 2018 12:23:23 +0100 Subject: [PATCH 02/16] Fix typo in src/librustc/ty/layout.rs Co-Authored-By: wildarch --- src/librustc/ty/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index a67aa0e41b3..c05e0aaf3ba 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1888,7 +1888,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> _ => { let mut data_variant = match this.variants { Variants::NicheFilling { dataful_variant, .. } => { - // Only the niche itthis is always initialized, + // Only the niche in this is always initialized, // so only check for a pointer at its offset. // // If the niche is a pointer, it's either valid From ed716d4c8960bb5e5127375ac023c698613b27bd Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Fri, 28 Dec 2018 12:52:31 +0100 Subject: [PATCH 03/16] Return instead of collecting to mut result. --- src/librustc/ty/layout.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index c05e0aaf3ba..472747a7170 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1833,17 +1833,19 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> }) } - fn pointee_info_at(this: TyLayout<'tcx>, cx: &C, offset: Size + fn pointee_info_at( + this: TyLayout<'tcx>, + cx: &C, + offset: Size, ) -> Option { - let mut result = None; match this.ty.sty { ty::RawPtr(mt) if offset.bytes() == 0 => { - result = cx.layout_of(mt.ty).ok() + cx.layout_of(mt.ty).ok() .map(|layout| PointeeInfo { size: layout.size, align: layout.align.abi, safe: None, - }); + }) } ty::Ref(_, ty, mt) if offset.bytes() == 0 => { @@ -1877,12 +1879,12 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } }; - result = cx.layout_of(ty).ok() + cx.layout_of(ty).ok() .map(|layout| PointeeInfo { size: layout.size, align: layout.align.abi, safe: Some(kind), - }); + }) } _ => { @@ -1915,6 +1917,8 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } } + let mut result = None; + if let Some(variant) = data_variant { let ptr_end = offset + Pointer.size(cx); for i in 0..variant.fields.count() { @@ -1945,10 +1949,10 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } } } + + result } } - - result } } From f1f9343c3df715392e1c0d28c8b2982d8dd6cf96 Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Fri, 28 Dec 2018 13:15:39 +0100 Subject: [PATCH 04/16] Remove old pointee_info_at body. --- src/librustc_codegen_llvm/abi.rs | 8 +- src/librustc_codegen_llvm/context.rs | 2 +- src/librustc_codegen_llvm/type_of.rs | 135 +-------------------------- 3 files changed, 13 insertions(+), 132 deletions(-) diff --git a/src/librustc_codegen_llvm/abi.rs b/src/librustc_codegen_llvm/abi.rs index 2c4a1ded97f..7a700882237 100644 --- a/src/librustc_codegen_llvm/abi.rs +++ b/src/librustc_codegen_llvm/abi.rs @@ -7,13 +7,19 @@ use crate::value::Value; use rustc_codegen_ssa::MemFlags; use rustc_codegen_ssa::mir::place::PlaceRef; use rustc_codegen_ssa::mir::operand::OperandValue; +<<<<<<< HEAD +======= +use type_::Type; +use type_of::{LayoutLlvmExt}; +use value::Value; +>>>>>>> Remove old pointee_info_at body. use rustc_target::abi::call::ArgType; use rustc_codegen_ssa::traits::*; use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TyLayout, Abi as LayoutAbi}; use rustc::ty::{self, Ty, Instance}; -use rustc::ty::layout; +use rustc::ty::layout::{self, PointerKind}; use libc::c_uint; diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index f6956bd5736..55b28682725 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -16,7 +16,7 @@ use rustc_data_structures::small_c_str::SmallCStr; use rustc::mir::mono::Stats; use rustc::session::config::{self, DebugInfo}; use rustc::session::Session; -use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout, VariantIdx}; +use rustc::ty::layout::{LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx}; use rustc::ty::{self, Ty, TyCtxt}; use rustc::util::nodemap::FxHashMap; use rustc_target::spec::{HasTargetSpec, Target}; diff --git a/src/librustc_codegen_llvm/type_of.rs b/src/librustc_codegen_llvm/type_of.rs index cbcc457fda9..e1c3c42add8 100644 --- a/src/librustc_codegen_llvm/type_of.rs +++ b/src/librustc_codegen_llvm/type_of.rs @@ -2,9 +2,11 @@ use crate::abi::{FnType, FnTypeExt}; use crate::common::*; use crate::type_::Type; use rustc::hir; +use abi::{FnType, FnTypeExt}; +use common::*; use rustc::ty::{self, Ty, TypeFoldable}; -use rustc::ty::layout::{self, Align, LayoutOf, Size, TyLayout}; -use rustc_target::abi::FloatTy; +use rustc::ty::layout::{self, Align, LayoutOf, PointeeInfo, Size, TyLayout}; +use rustc_target::abi::{FloatTy, TyLayoutMethods}; use rustc_mir::monomorphize::item::DefPathBasedNames; use rustc_codegen_ssa::traits::*; @@ -174,28 +176,6 @@ impl<'a, 'tcx> CodegenCx<'a, 'tcx> { } } -#[derive(Copy, Clone, PartialEq, Eq)] -pub enum PointerKind { - /// Most general case, we know no restrictions to tell LLVM. - Shared, - - /// `&T` where `T` contains no `UnsafeCell`, is `noalias` and `readonly`. - Frozen, - - /// `&mut T`, when we know `noalias` is safe for LLVM. - UniqueBorrowed, - - /// `Box`, unlike `UniqueBorrowed`, it also has `noalias` on returns. - UniqueOwned -} - -#[derive(Copy, Clone)] -pub struct PointeeInfo { - pub size: Size, - pub align: Align, - pub safe: Option, -} - pub trait LayoutLlvmExt<'tcx> { fn is_llvm_immediate(&self) -> bool; fn is_llvm_scalar_pair<'a>(&self) -> bool; @@ -406,112 +386,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> { return pointee; } - let mut result = None; - match self.ty.sty { - ty::RawPtr(mt) if offset.bytes() == 0 => { - let (size, align) = cx.size_and_align_of(mt.ty); - result = Some(PointeeInfo { - size, - align, - safe: None - }); - } - - ty::Ref(_, ty, mt) if offset.bytes() == 0 => { - let (size, align) = cx.size_and_align_of(ty); - - let kind = match mt { - hir::MutImmutable => if cx.type_is_freeze(ty) { - PointerKind::Frozen - } else { - PointerKind::Shared - }, - hir::MutMutable => { - // Previously we would only emit noalias annotations for LLVM >= 6 or in - // panic=abort mode. That was deemed right, as prior versions had many bugs - // in conjunction with unwinding, but later versions didn’t seem to have - // said issues. See issue #31681. - // - // Alas, later on we encountered a case where noalias would generate wrong - // code altogether even with recent versions of LLVM in *safe* code with no - // unwinding involved. See #54462. - // - // For now, do not enable mutable_noalias by default at all, while the - // issue is being figured out. - let mutable_noalias = cx.tcx.sess.opts.debugging_opts.mutable_noalias - .unwrap_or(false); - if mutable_noalias { - PointerKind::UniqueBorrowed - } else { - PointerKind::Shared - } - } - }; - - result = Some(PointeeInfo { - size, - align, - safe: Some(kind) - }); - } - - _ => { - let mut data_variant = match self.variants { - // Within the discriminant field, only the niche itself is - // always initialized, so we only check for a pointer at its - // offset. - // - // If the niche is a pointer, it's either valid (according - // to its type), or null (which the niche field's scalar - // validity range encodes). This allows using - // `dereferenceable_or_null` for e.g., `Option<&T>`, and - // this will continue to work as long as we don't start - // using more niches than just null (e.g., the first page of - // the address space, or unaligned pointers). - layout::Variants::Multiple { - discr_kind: layout::DiscriminantKind::Niche { - dataful_variant, - .. - }, - discr_index, - .. - } if self.fields.offset(discr_index) == offset => - Some(self.for_variant(cx, dataful_variant)), - _ => Some(*self), - }; - - if let Some(variant) = data_variant { - // We're not interested in any unions. - if let layout::FieldPlacement::Union(_) = variant.fields { - data_variant = None; - } - } - - if let Some(variant) = data_variant { - let ptr_end = offset + layout::Pointer.size(cx); - for i in 0..variant.fields.count() { - let field_start = variant.fields.offset(i); - if field_start <= offset { - let field = variant.field(cx, i); - if ptr_end <= field_start + field.size { - // We found the right field, look inside it. - result = field.pointee_info_at(cx, offset - field_start); - break; - } - } - } - } - - // FIXME(eddyb) This should be for `ptr::Unique`, not `Box`. - if let Some(ref mut pointee) = result { - if let ty::Adt(def, _) = self.ty.sty { - if def.is_box() && offset.bytes() == 0 { - pointee.safe = Some(PointerKind::UniqueOwned); - } - } - } - } - } + let result = Ty::pointee_info_at(*self, cx, offset); cx.pointee_infos.borrow_mut().insert((self.ty, offset), result); result From d47ec57a4faf627a10058e31abecf5786bd799b3 Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Fri, 28 Dec 2018 15:19:23 +0100 Subject: [PATCH 05/16] Add param_env parameter to pointee_info_at. An associated type ParamEnv has been added to TyLayoutMethods to facilitate this. --- src/librustc/ty/layout.rs | 7 +++++-- src/librustc_codegen_llvm/type_of.rs | 2 +- src/librustc_target/abi/mod.rs | 5 ++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 472747a7170..0473d3d02f8 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1666,6 +1666,8 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> where C: LayoutOf> + HasTyCtxt<'tcx>, C::TyLayout: MaybeResult> { + type ParamEnv = ty::ParamEnv<'tcx>; + fn for_variant(this: TyLayout<'tcx>, cx: &C, variant_index: VariantIdx) -> TyLayout<'tcx> { let details = match this.variants { Variants::Single { index } if index == variant_index => this.details, @@ -1837,6 +1839,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> this: TyLayout<'tcx>, cx: &C, offset: Size, + param_env: Self::ParamEnv, ) -> Option { match this.ty.sty { ty::RawPtr(mt) if offset.bytes() == 0 => { @@ -1850,7 +1853,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> ty::Ref(_, ty, mt) if offset.bytes() == 0 => { let tcx = cx.tcx(); - let is_freeze = ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP); + let is_freeze = ty.is_freeze(tcx, param_env, DUMMY_SP); let kind = match mt { hir::MutImmutable => if is_freeze { PointerKind::Frozen @@ -1929,7 +1932,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> .and_then(|field| { if ptr_end <= field_start + field.size { // We found the right field, look inside it. - Self::pointee_info_at(field, cx, offset - field_start) + Self::pointee_info_at(field, cx, offset - field_start, param_env) } else { None } diff --git a/src/librustc_codegen_llvm/type_of.rs b/src/librustc_codegen_llvm/type_of.rs index e1c3c42add8..fd15fdf2176 100644 --- a/src/librustc_codegen_llvm/type_of.rs +++ b/src/librustc_codegen_llvm/type_of.rs @@ -386,7 +386,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> { return pointee; } - let result = Ty::pointee_info_at(*self, cx, offset); + let result = Ty::pointee_info_at(*self, cx, offset, ty::ParamEnv::reveal_all()); cx.pointee_infos.borrow_mut().insert((self.ty, offset), result); result diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 74257cb3f64..1cdef7b0064 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -933,6 +933,8 @@ pub struct PointeeInfo { } pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { + type ParamEnv; + fn for_variant( this: TyLayout<'a, Self>, cx: &C, @@ -942,7 +944,8 @@ pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { fn pointee_info_at( this: TyLayout<'a, Self>, cx: &C, - offset: Size + offset: Size, + param_env: Self::ParamEnv, ) -> Option; } From 7257fc34de8c2c66cf0fa1d628af54f50d368caf Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Fri, 11 Jan 2019 11:26:47 +0100 Subject: [PATCH 06/16] Make line fit within 100 character limit. --- src/librustc/ty/layout.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 0473d3d02f8..3ad316f8f6d 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1931,8 +1931,9 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> result = field.ok() .and_then(|field| { if ptr_end <= field_start + field.size { + let off = offset - field_start; // We found the right field, look inside it. - Self::pointee_info_at(field, cx, offset - field_start, param_env) + Self::pointee_info_at(field, cx, off, param_env) } else { None } From 82410e800f864684ec5009ea0cecd9614155ca06 Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Mon, 11 Mar 2019 21:26:49 +0100 Subject: [PATCH 07/16] impl `pointee_info_at` in TyLayout. --- src/librustc_codegen_llvm/abi.rs | 4 ++-- src/librustc_target/abi/mod.rs | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/librustc_codegen_llvm/abi.rs b/src/librustc_codegen_llvm/abi.rs index 7a700882237..a59f756570f 100644 --- a/src/librustc_codegen_llvm/abi.rs +++ b/src/librustc_codegen_llvm/abi.rs @@ -18,7 +18,7 @@ use rustc_target::abi::call::ArgType; use rustc_codegen_ssa::traits::*; use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TyLayout, Abi as LayoutAbi}; -use rustc::ty::{self, Ty, Instance}; +use rustc::ty::{self, Ty, Instance, ParamEnv}; use rustc::ty::layout::{self, PointerKind}; use libc::c_uint; @@ -484,7 +484,7 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> { } } - if let Some(pointee) = layout.pointee_info_at(cx, offset) { + if let Some(pointee) = layout.pointee_info_at(cx, offset, ParamEnv::reveal_all()) { if let Some(kind) = pointee.safe { attrs.pointee_size = pointee.size; attrs.pointee_align = Some(pointee.align); diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 1cdef7b0064..6dd622b3f0f 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -958,6 +958,10 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::field(self, cx, i) } + pub fn pointee_info_at(self, cx: &C, offset: Size, param_env: Ty::ParamEnv) -> Option + where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { + Ty::pointee_info_at(self, cx, offset, param_env) + } } impl<'a, Ty> TyLayout<'a, Ty> { From 199ff02dacae8a00902bbd2bca3ec65652444598 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Wed, 24 Apr 2019 20:29:46 +0530 Subject: [PATCH 08/16] resolving conflicts --- src/librustc/ty/layout.rs | 45 ++++++++++++++-------------- src/librustc_codegen_llvm/abi.rs | 8 +---- src/librustc_codegen_llvm/context.rs | 1 - src/librustc_codegen_llvm/type_of.rs | 3 -- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 3ad316f8f6d..b0631598554 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -12,7 +12,7 @@ use std::iter; use std::mem; use std::ops::Bound; -use hir; +use crate::hir; use crate::ich::StableHashingContext; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, @@ -1892,25 +1892,27 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> _ => { let mut data_variant = match this.variants { - Variants::NicheFilling { dataful_variant, .. } => { - // Only the niche in this is always initialized, - // so only check for a pointer at its offset. - // - // If the niche is a pointer, it's either valid - // (according to its type), or null (which the - // niche field's scalar validity range encodes). - // This allows using `dereferenceable_or_null` - // for e.g., `Option<&T>`, and this will continue - // to work as long as we don't start using more - // niches than just null (e.g., the first page - // of the address space, or unaligned pointers). - if this.fields.offset(0) == offset { - Some(this.for_variant(cx, dataful_variant)) - } else { - None - } - } - _ => Some(this) + // Within the discriminant field, only the niche itself is + // always initialized, so we only check for a pointer at its + // offset. + // + // If the niche is a pointer, it's either valid (according + // to its type), or null (which the niche field's scalar + // validity range encodes). This allows using + // `dereferenceable_or_null` for e.g., `Option<&T>`, and + // this will continue to work as long as we don't start + // using more niches than just null (e.g., the first page of + // the address space, or unaligned pointers). + Variants::Multiple { + discr_kind: DiscriminantKind::Niche { + dataful_variant, + .. + }, + discr_index, + .. + } if this.fields.offset(discr_index) == offset => + Some(this.for_variant(cx, dataful_variant)), + _ => Some(this), }; if let Some(variant) = data_variant { @@ -1931,9 +1933,8 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> result = field.ok() .and_then(|field| { if ptr_end <= field_start + field.size { - let off = offset - field_start; // We found the right field, look inside it. - Self::pointee_info_at(field, cx, off, param_env) + field.pointee_info_at(cx, offset - field_start, param_env) } else { None } diff --git a/src/librustc_codegen_llvm/abi.rs b/src/librustc_codegen_llvm/abi.rs index a59f756570f..1be07367914 100644 --- a/src/librustc_codegen_llvm/abi.rs +++ b/src/librustc_codegen_llvm/abi.rs @@ -2,17 +2,11 @@ use crate::llvm::{self, AttributePlace}; use crate::builder::Builder; use crate::context::CodegenCx; use crate::type_::Type; -use crate::type_of::{LayoutLlvmExt, PointerKind}; use crate::value::Value; +use crate::type_of::{LayoutLlvmExt}; use rustc_codegen_ssa::MemFlags; use rustc_codegen_ssa::mir::place::PlaceRef; use rustc_codegen_ssa::mir::operand::OperandValue; -<<<<<<< HEAD -======= -use type_::Type; -use type_of::{LayoutLlvmExt}; -use value::Value; ->>>>>>> Remove old pointee_info_at body. use rustc_target::abi::call::ArgType; use rustc_codegen_ssa::traits::*; diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index 55b28682725..7cf78a41feb 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -8,7 +8,6 @@ use rustc::hir; use crate::monomorphize::partitioning::CodegenUnit; use crate::type_::Type; -use crate::type_of::PointeeInfo; use rustc_codegen_ssa::traits::*; use rustc_data_structures::base_n; diff --git a/src/librustc_codegen_llvm/type_of.rs b/src/librustc_codegen_llvm/type_of.rs index fd15fdf2176..3f717754e32 100644 --- a/src/librustc_codegen_llvm/type_of.rs +++ b/src/librustc_codegen_llvm/type_of.rs @@ -1,9 +1,6 @@ use crate::abi::{FnType, FnTypeExt}; use crate::common::*; use crate::type_::Type; -use rustc::hir; -use abi::{FnType, FnTypeExt}; -use common::*; use rustc::ty::{self, Ty, TypeFoldable}; use rustc::ty::layout::{self, Align, LayoutOf, PointeeInfo, Size, TyLayout}; use rustc_target::abi::{FloatTy, TyLayoutMethods}; From 35bd58b4e85791e9c94f1eea7387f59c5feb6b60 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Wed, 24 Apr 2019 21:25:56 +0530 Subject: [PATCH 09/16] tidy fixes --- src/librustc_target/abi/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 6dd622b3f0f..d69b4b6d2bd 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -958,7 +958,9 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::field(self, cx, i) } - pub fn pointee_info_at(self, cx: &C, offset: Size, param_env: Ty::ParamEnv) -> Option + pub fn pointee_info_at( + self, cx: &C, offset: Size, param_env: Ty::ParamEnv + ) -> Option where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::pointee_info_at(self, cx, offset, param_env) } From 8e3d9f1039df40a959d8d63e71dec5ecfecd56c7 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Thu, 25 Apr 2019 18:22:34 +0530 Subject: [PATCH 10/16] add to_result to ty::MaybeResult --- src/librustc/ty/layout.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index b0631598554..7f879cd77fd 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -5,6 +5,7 @@ use syntax::ast::{self, Ident, IntTy, UintTy}; use syntax::attr; use syntax_pos::DUMMY_SP; +// use std::convert::From; use std::cmp; use std::fmt; use std::i128; @@ -1544,32 +1545,38 @@ impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> { } pub trait MaybeResult { + type Item; + fn from_ok(x: T) -> Self; fn map_same T>(self, f: F) -> Self; - fn ok(self) -> Option; + fn to_result(self) -> Result; } impl MaybeResult for T { + type Item = !; + fn from_ok(x: T) -> Self { x } fn map_same T>(self, f: F) -> Self { f(self) } - fn ok(self) -> Option { - Some(self) + fn to_result(self) -> Result { + Ok(self) } } impl MaybeResult for Result { + type Item = E; + fn from_ok(x: T) -> Self { Ok(x) } fn map_same T>(self, f: F) -> Self { self.map(f) } - fn ok(self) -> Option { - self.ok() + fn to_result(self) -> Result { + self } } @@ -1843,7 +1850,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> ) -> Option { match this.ty.sty { ty::RawPtr(mt) if offset.bytes() == 0 => { - cx.layout_of(mt.ty).ok() + cx.layout_of(mt.ty).to_result().ok() .map(|layout| PointeeInfo { size: layout.size, align: layout.align.abi, @@ -1882,7 +1889,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } }; - cx.layout_of(ty).ok() + cx.layout_of(ty).to_result().ok() .map(|layout| PointeeInfo { size: layout.size, align: layout.align.abi, @@ -1930,7 +1937,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> let field_start = variant.fields.offset(i); if field_start <= offset { let field = variant.field(cx, i); - result = field.ok() + result = field.to_result().ok() .and_then(|field| { if ptr_end <= field_start + field.size { // We found the right field, look inside it. From 852dd491efde37dfecacd803d843592a15f9eab6 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Tue, 30 Apr 2019 12:19:53 +0530 Subject: [PATCH 11/16] removing map_same from MaybeResult --- src/librustc/ty/layout.rs | 44 +++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 7f879cd77fd..319db52fe20 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -5,7 +5,6 @@ use syntax::ast::{self, Ident, IntTy, UintTy}; use syntax::attr; use syntax_pos::DUMMY_SP; -// use std::convert::From; use std::cmp; use std::fmt; use std::i128; @@ -1545,37 +1544,31 @@ impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> { } pub trait MaybeResult { - type Item; + type Error; - fn from_ok(x: T) -> Self; - fn map_same T>(self, f: F) -> Self; - fn to_result(self) -> Result; + fn from(x: Result) -> Self; + fn to_result(self) -> Result; } impl MaybeResult for T { - type Item = !; + type Error = !; - fn from_ok(x: T) -> Self { + fn from(x: Result) -> Self { + let Ok(x) = x; x } - fn map_same T>(self, f: F) -> Self { - f(self) - } - fn to_result(self) -> Result { + fn to_result(self) -> Result { Ok(self) } } impl MaybeResult for Result { - type Item = E; + type Error = E; - fn from_ok(x: T) -> Self { - Ok(x) + fn from(x: Result) -> Self { + x } - fn map_same T>(self, f: F) -> Self { - self.map(f) - } - fn to_result(self) -> Result { + fn to_result(self) -> Result { self } } @@ -1681,10 +1674,9 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> Variants::Single { index } => { // Deny calling for_variant more than once for non-Single enums. - cx.layout_of(this.ty).map_same(|layout| { + if let Ok(layout) = cx.layout_of(this.ty).to_result() { assert_eq!(layout.variants, Variants::Single { index }); - layout - }); + } let fields = match this.ty.sty { ty::Adt(def, _) => def.variants[variant_index].fields.len(), @@ -1754,10 +1746,12 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } else { tcx.mk_mut_ref(tcx.lifetimes.re_static, nil) }; - return cx.layout_of(ptr_ty).map_same(|mut ptr_layout| { - ptr_layout.ty = this.ty; - ptr_layout - }); + return MaybeResult::from( + cx.layout_of(ptr_ty).to_result().map(|mut ptr_layout| { + ptr_layout.ty = this.ty; + ptr_layout + }) + ); } match tcx.struct_tail(pointee).sty { From 8802dc037eb2fa84692588bc026bd550554e282b Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Fri, 3 May 2019 13:05:10 +0530 Subject: [PATCH 12/16] adding is_freeze to TyLayoutMethods --- src/librustc/ty/layout.rs | 8 ++++++++ src/librustc_target/abi/mod.rs | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 319db52fe20..e4f80838d84 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1961,6 +1961,14 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } } + fn is_freeze( + this: TyLayout<'tcx>, + cx: &C, + param_env: Self::ParamEnv, + )-> bool { + this.ty.is_freeze(cx.tcx(), param_env, DUMMY_SP) + } + } struct Niche { diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index d69b4b6d2bd..087d98d7031 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -947,6 +947,11 @@ pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { offset: Size, param_env: Self::ParamEnv, ) -> Option; + fn is_freeze( + this: TyLayout<'a, Self>, + cx: &C, + param_env: Self::ParamEnv, + )-> bool; } impl<'a, Ty> TyLayout<'a, Ty> { @@ -964,6 +969,10 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::pointee_info_at(self, cx, offset, param_env) } + pub fn is_freeze(self, cx: &C, param_env: Ty::ParamEnv) -> bool + where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { + Ty::is_freeze(self, cx, param_env) + } } impl<'a, Ty> TyLayout<'a, Ty> { From 18679cdc54c91140499f4113f2d1122f200ac7fb Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Sat, 4 May 2019 15:02:22 +0530 Subject: [PATCH 13/16] adding HasParamEnv trait --- src/librustc/ty/layout.rs | 22 ++++++++++++---------- src/librustc_codegen_llvm/builder.rs | 6 ++++++ src/librustc_codegen_llvm/context.rs | 8 +++++++- src/librustc_codegen_ssa/traits/builder.rs | 4 +++- src/librustc_codegen_ssa/traits/mod.rs | 7 ++++++- src/librustc_mir/interpret/eval_context.rs | 8 ++++++++ src/librustc_passes/layout_test.rs | 7 +++++++ src/librustc_target/abi/mod.rs | 9 --------- 8 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index e4f80838d84..d4b93ed4785 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1662,9 +1662,20 @@ impl ty::query::TyCtxtAt<'a, 'tcx, '_> { } } +pub trait HasParamEnv<'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx>; +} + +impl<'tcx, C> HasParamEnv<'tcx> for LayoutCx<'tcx, C> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + self.param_env + } +} + impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> where C: LayoutOf> + HasTyCtxt<'tcx>, - C::TyLayout: MaybeResult> + C::TyLayout: MaybeResult>, + C: HasParamEnv<'tcx> { type ParamEnv = ty::ParamEnv<'tcx>; @@ -1960,15 +1971,6 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } } } - - fn is_freeze( - this: TyLayout<'tcx>, - cx: &C, - param_env: Self::ParamEnv, - )-> bool { - this.ty.is_freeze(cx.tcx(), param_env, DUMMY_SP) - } - } struct Niche { diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs index 123fda1e215..bc2bb97a19e 100644 --- a/src/librustc_codegen_llvm/builder.rs +++ b/src/librustc_codegen_llvm/builder.rs @@ -66,6 +66,12 @@ impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> { } } +impl ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + self.cx.param_env() + } +} + impl ty::layout::LayoutOf for Builder<'_, '_, 'tcx> { type Ty = Ty<'tcx>; type TyLayout = TyLayout<'tcx>; diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index 7cf78a41feb..a225a11e94d 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -15,7 +15,7 @@ use rustc_data_structures::small_c_str::SmallCStr; use rustc::mir::mono::Stats; use rustc::session::config::{self, DebugInfo}; use rustc::session::Session; -use rustc::ty::layout::{LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx}; +use rustc::ty::layout::{LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx, HasParamEnv}; use rustc::ty::{self, Ty, TyCtxt}; use rustc::util::nodemap::FxHashMap; use rustc_target::spec::{HasTargetSpec, Target}; @@ -861,3 +861,9 @@ impl LayoutOf for CodegenCx<'ll, 'tcx> { }) } } + +impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + panic!("asd") + } +} diff --git a/src/librustc_codegen_ssa/traits/builder.rs b/src/librustc_codegen_ssa/traits/builder.rs index 48142fc9fa9..a3f99cd869e 100644 --- a/src/librustc_codegen_ssa/traits/builder.rs +++ b/src/librustc_codegen_ssa/traits/builder.rs @@ -10,7 +10,7 @@ use crate::mir::operand::OperandRef; use crate::mir::place::PlaceRef; use crate::MemFlags; use rustc::ty::Ty; -use rustc::ty::layout::{Align, Size}; +use rustc::ty::layout::{Align, Size, HasParamEnv}; use std::ops::Range; use std::iter::TrustedLen; @@ -29,6 +29,8 @@ pub trait BuilderMethods<'a, 'tcx: 'a>: + IntrinsicCallMethods<'tcx> + AsmBuilderMethods<'tcx> + StaticBuilderMethods<'tcx> + + HasParamEnv<'tcx> + { fn new_block<'b>(cx: &'a Self::CodegenCx, llfn: Self::Value, name: &'b str) -> Self; fn with_cx(cx: &'a Self::CodegenCx) -> Self; diff --git a/src/librustc_codegen_ssa/traits/mod.rs b/src/librustc_codegen_ssa/traits/mod.rs index 8fe8b7ecd47..8c336b11a5d 100644 --- a/src/librustc_codegen_ssa/traits/mod.rs +++ b/src/librustc_codegen_ssa/traits/mod.rs @@ -41,6 +41,8 @@ pub use self::type_::{ ArgTypeMethods, BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods, TypeMethods, }; pub use self::write::{ModuleBufferMethods, ThinBufferMethods, WriteBackendMethods}; +use rustc::ty::layout::{HasParamEnv}; + use std::fmt; @@ -58,6 +60,7 @@ pub trait CodegenMethods<'tcx>: + DeclareMethods<'tcx> + AsmMethods<'tcx> + PreDefineMethods<'tcx> + + HasParamEnv<'tcx> { } @@ -72,6 +75,7 @@ impl<'tcx, T> CodegenMethods<'tcx> for T where + DeclareMethods<'tcx> + AsmMethods<'tcx> + PreDefineMethods<'tcx> + + HasParamEnv<'tcx> { } @@ -85,5 +89,6 @@ pub trait HasCodegen<'tcx>: Type = Self::Type, Funclet = Self::Funclet, DIScope = Self::DIScope, - >; + > + + HasParamEnv<'tcx>; } diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index ad4bc6a91f5..db827afdb94 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -175,6 +175,14 @@ impl<'a, 'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for InterpretCx<'a, 'mir, 'tcx, } } +impl<'a, 'mir, 'tcx, M> layout::HasParamEnv<'tcx> for InterpretCx<'a, 'mir, 'tcx, M> + where M: Machine<'a, 'mir, 'tcx> +{ + fn param_env(&self) -> ty::ParamEnv<'tcx> { + self.param_env + } +} + impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> LayoutOf for InterpretCx<'a, 'mir, 'tcx, M> { diff --git a/src/librustc_passes/layout_test.rs b/src/librustc_passes/layout_test.rs index 6940f8f442e..7041a5593ab 100644 --- a/src/librustc_passes/layout_test.rs +++ b/src/librustc_passes/layout_test.rs @@ -7,6 +7,7 @@ use rustc::ty::layout::HasTyCtxt; use rustc::ty::layout::LayoutOf; use rustc::ty::layout::TargetDataLayout; use rustc::ty::layout::TyLayout; +use rustc::ty::layout::HasParamEnv; use rustc::ty::ParamEnv; use rustc::ty::Ty; use rustc::ty::TyCtxt; @@ -122,6 +123,12 @@ impl<'me, 'tcx> HasTyCtxt<'tcx> for UnwrapLayoutCx<'me, 'tcx> { } } +impl<'me, 'tcx> HasParamEnv<'tcx> for UnwrapLayoutCx<'me, 'tcx> { + fn param_env(&self) -> ParamEnv<'tcx> { + self.param_env + } +} + impl<'me, 'tcx> HasDataLayout for UnwrapLayoutCx<'me, 'tcx> { fn data_layout(&self) -> &TargetDataLayout { self.tcx.data_layout() diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 087d98d7031..d69b4b6d2bd 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -947,11 +947,6 @@ pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { offset: Size, param_env: Self::ParamEnv, ) -> Option; - fn is_freeze( - this: TyLayout<'a, Self>, - cx: &C, - param_env: Self::ParamEnv, - )-> bool; } impl<'a, Ty> TyLayout<'a, Ty> { @@ -969,10 +964,6 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::pointee_info_at(self, cx, offset, param_env) } - pub fn is_freeze(self, cx: &C, param_env: Ty::ParamEnv) -> bool - where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { - Ty::is_freeze(self, cx, param_env) - } } impl<'a, Ty> TyLayout<'a, Ty> { From 94a48924da05d2c3c147ce7739d497fdcf33ca01 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Sat, 4 May 2019 16:10:47 +0530 Subject: [PATCH 14/16] resolving conflicts --- src/librustc/ty/layout.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index d4b93ed4785..dc9eaf5592e 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1720,10 +1720,9 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> let tcx = cx.tcx(); let discr_layout = |discr: &Scalar| -> C::TyLayout { let layout = LayoutDetails::scalar(cx, discr.clone()); - MaybeResult::from_ok(TyLayout { - details: tcx.intern_layout(layout), - ty: discr.value.to_ty(tcx) - }) + MaybeResult::from(Ok( + TyLayout {details: tcx.intern_layout(layout),ty: discr.value.to_ty(tcx)} + )) }; cx.layout_of(match this.ty.sty { From 80d5478649b5c26846afb1a923c24f8103b887cf Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Sat, 4 May 2019 18:06:40 +0530 Subject: [PATCH 15/16] removing param_env from pointee_info_at --- src/librustc/ty/layout.rs | 7 ++----- src/librustc_codegen_llvm/abi.rs | 4 ++-- src/librustc_codegen_llvm/context.rs | 6 ++++-- src/librustc_codegen_llvm/type_of.rs | 2 +- src/librustc_target/abi/mod.rs | 9 ++------- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index dc9eaf5592e..9a5db202296 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1677,8 +1677,6 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> C::TyLayout: MaybeResult>, C: HasParamEnv<'tcx> { - type ParamEnv = ty::ParamEnv<'tcx>; - fn for_variant(this: TyLayout<'tcx>, cx: &C, variant_index: VariantIdx) -> TyLayout<'tcx> { let details = match this.variants { Variants::Single { index } if index == variant_index => this.details, @@ -1850,7 +1848,6 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> this: TyLayout<'tcx>, cx: &C, offset: Size, - param_env: Self::ParamEnv, ) -> Option { match this.ty.sty { ty::RawPtr(mt) if offset.bytes() == 0 => { @@ -1864,7 +1861,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> ty::Ref(_, ty, mt) if offset.bytes() == 0 => { let tcx = cx.tcx(); - let is_freeze = ty.is_freeze(tcx, param_env, DUMMY_SP); + let is_freeze = ty.is_freeze(tcx, cx.param_env(), DUMMY_SP); let kind = match mt { hir::MutImmutable => if is_freeze { PointerKind::Frozen @@ -1945,7 +1942,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> .and_then(|field| { if ptr_end <= field_start + field.size { // We found the right field, look inside it. - field.pointee_info_at(cx, offset - field_start, param_env) + field.pointee_info_at(cx, offset - field_start) } else { None } diff --git a/src/librustc_codegen_llvm/abi.rs b/src/librustc_codegen_llvm/abi.rs index 1be07367914..70d184240fc 100644 --- a/src/librustc_codegen_llvm/abi.rs +++ b/src/librustc_codegen_llvm/abi.rs @@ -12,7 +12,7 @@ use rustc_target::abi::call::ArgType; use rustc_codegen_ssa::traits::*; use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TyLayout, Abi as LayoutAbi}; -use rustc::ty::{self, Ty, Instance, ParamEnv}; +use rustc::ty::{self, Ty, Instance}; use rustc::ty::layout::{self, PointerKind}; use libc::c_uint; @@ -478,7 +478,7 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> { } } - if let Some(pointee) = layout.pointee_info_at(cx, offset, ParamEnv::reveal_all()) { + if let Some(pointee) = layout.pointee_info_at(cx, offset) { if let Some(kind) = pointee.safe { attrs.pointee_size = pointee.size; attrs.pointee_align = Some(pointee.align); diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index a225a11e94d..7bf8f705ea8 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -15,7 +15,9 @@ use rustc_data_structures::small_c_str::SmallCStr; use rustc::mir::mono::Stats; use rustc::session::config::{self, DebugInfo}; use rustc::session::Session; -use rustc::ty::layout::{LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx, HasParamEnv}; +use rustc::ty::layout::{ + LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx, HasParamEnv +}; use rustc::ty::{self, Ty, TyCtxt}; use rustc::util::nodemap::FxHashMap; use rustc_target::spec::{HasTargetSpec, Target}; @@ -864,6 +866,6 @@ impl LayoutOf for CodegenCx<'ll, 'tcx> { impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> { fn param_env(&self) -> ty::ParamEnv<'tcx> { - panic!("asd") + ty::ParamEnv::reveal_all() } } diff --git a/src/librustc_codegen_llvm/type_of.rs b/src/librustc_codegen_llvm/type_of.rs index 3f717754e32..ff25ed92566 100644 --- a/src/librustc_codegen_llvm/type_of.rs +++ b/src/librustc_codegen_llvm/type_of.rs @@ -383,7 +383,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> { return pointee; } - let result = Ty::pointee_info_at(*self, cx, offset, ty::ParamEnv::reveal_all()); + let result = Ty::pointee_info_at(*self, cx, offset); cx.pointee_infos.borrow_mut().insert((self.ty, offset), result); result diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index d69b4b6d2bd..4b61057e5cf 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -933,8 +933,6 @@ pub struct PointeeInfo { } pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { - type ParamEnv; - fn for_variant( this: TyLayout<'a, Self>, cx: &C, @@ -945,7 +943,6 @@ pub trait TyLayoutMethods<'a, C: LayoutOf>: Sized { this: TyLayout<'a, Self>, cx: &C, offset: Size, - param_env: Self::ParamEnv, ) -> Option; } @@ -958,11 +955,9 @@ impl<'a, Ty> TyLayout<'a, Ty> { where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { Ty::field(self, cx, i) } - pub fn pointee_info_at( - self, cx: &C, offset: Size, param_env: Ty::ParamEnv - ) -> Option + pub fn pointee_info_at(self, cx: &C, offset: Size) -> Option where Ty: TyLayoutMethods<'a, C>, C: LayoutOf { - Ty::pointee_info_at(self, cx, offset, param_env) + Ty::pointee_info_at(self, cx, offset) } } From 968eb7ff5a5db0d842527cba15be6dba741103a2 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Sun, 5 May 2019 23:39:04 +0530 Subject: [PATCH 16/16] code review fixes --- src/librustc/ty/layout.rs | 37 +++++++++++++------------- src/librustc_codegen_ssa/traits/mod.rs | 3 +-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 9a5db202296..d1a8a9a34e1 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -1519,6 +1519,10 @@ pub trait HasTyCtxt<'tcx>: HasDataLayout { fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx>; } +pub trait HasParamEnv<'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx>; +} + impl<'a, 'gcx, 'tcx> HasDataLayout for TyCtxt<'a, 'gcx, 'tcx> { fn data_layout(&self) -> &TargetDataLayout { &self.data_layout @@ -1531,6 +1535,12 @@ impl<'a, 'gcx, 'tcx> HasTyCtxt<'gcx> for TyCtxt<'a, 'gcx, 'tcx> { } } +impl<'tcx, C> HasParamEnv<'tcx> for LayoutCx<'tcx, C> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + self.param_env + } +} + impl<'tcx, T: HasDataLayout> HasDataLayout for LayoutCx<'tcx, T> { fn data_layout(&self) -> &TargetDataLayout { self.tcx.data_layout() @@ -1662,16 +1672,6 @@ impl ty::query::TyCtxtAt<'a, 'tcx, '_> { } } -pub trait HasParamEnv<'tcx> { - fn param_env(&self) -> ty::ParamEnv<'tcx>; -} - -impl<'tcx, C> HasParamEnv<'tcx> for LayoutCx<'tcx, C> { - fn param_env(&self) -> ty::ParamEnv<'tcx> { - self.param_env - } -} - impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> where C: LayoutOf> + HasTyCtxt<'tcx>, C::TyLayout: MaybeResult>, @@ -1718,9 +1718,10 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> let tcx = cx.tcx(); let discr_layout = |discr: &Scalar| -> C::TyLayout { let layout = LayoutDetails::scalar(cx, discr.clone()); - MaybeResult::from(Ok( - TyLayout {details: tcx.intern_layout(layout),ty: discr.value.to_ty(tcx)} - )) + MaybeResult::from(Ok(TyLayout { + details: tcx.intern_layout(layout), + ty: discr.value.to_ty(tcx), + })) }; cx.layout_of(match this.ty.sty { @@ -1754,12 +1755,10 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } else { tcx.mk_mut_ref(tcx.lifetimes.re_static, nil) }; - return MaybeResult::from( - cx.layout_of(ptr_ty).to_result().map(|mut ptr_layout| { - ptr_layout.ty = this.ty; - ptr_layout - }) - ); + return MaybeResult::from(cx.layout_of(ptr_ty).to_result().map(|mut ptr_layout| { + ptr_layout.ty = this.ty; + ptr_layout + })); } match tcx.struct_tail(pointee).sty { diff --git a/src/librustc_codegen_ssa/traits/mod.rs b/src/librustc_codegen_ssa/traits/mod.rs index 8c336b11a5d..c237cd8bd26 100644 --- a/src/librustc_codegen_ssa/traits/mod.rs +++ b/src/librustc_codegen_ssa/traits/mod.rs @@ -89,6 +89,5 @@ pub trait HasCodegen<'tcx>: Type = Self::Type, Funclet = Self::Funclet, DIScope = Self::DIScope, - > - + HasParamEnv<'tcx>; + >; }