Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 4 pull requests #71011

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn uncached_llvm_type<'a, 'tcx>(
};

match layout.fields {
FieldsShape::Union(_) => {
FieldsShape::Primitive | FieldsShape::Union(_) => {
let fill = cx.type_padding_filler(layout.size, layout.align.abi);
let packed = false;
match name {
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
_ => {}
}
match self.fields {
FieldsShape::Union(_) => {
FieldsShape::Primitive | FieldsShape::Union(_) => {
bug!("TyAndLayout::llvm_field_index({:?}): not applicable", self)
}

Expand Down
6 changes: 6 additions & 0 deletions src/librustc_data_structures/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
}
}

impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher)
}
}

impl<CTX> HashStable<CTX> for f32 {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let val: u32 = unsafe { ::std::mem::transmute(*self) };
Expand Down
27 changes: 24 additions & 3 deletions src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ struct HandlerInner {
/// The stashed diagnostics count towards the total error count.
/// When `.abort_if_errors()` is called, these are also emitted.
stashed_diagnostics: FxIndexMap<(Span, StashKey), Diagnostic>,

/// The warning count, used for a recap upon finishing
deduplicated_warn_count: usize,
}

/// A key denoting where from a diagnostic was stashed.
Expand Down Expand Up @@ -414,6 +417,7 @@ impl Handler {
flags,
err_count: 0,
deduplicated_err_count: 0,
deduplicated_warn_count: 0,
emitter,
delayed_span_bugs: Vec::new(),
taught_diagnostics: Default::default(),
Expand All @@ -439,6 +443,7 @@ impl Handler {
let mut inner = self.inner.borrow_mut();
inner.err_count = 0;
inner.deduplicated_err_count = 0;
inner.deduplicated_warn_count = 0;

// actually free the underlying memory (which `clear` would not do)
inner.delayed_span_bugs = Default::default();
Expand Down Expand Up @@ -745,6 +750,8 @@ impl HandlerInner {
self.emitter.emit_diagnostic(diagnostic);
if diagnostic.is_error() {
self.deduplicated_err_count += 1;
} else if diagnostic.level == Warning {
self.deduplicated_warn_count += 1;
}
}
if diagnostic.is_error() {
Expand All @@ -763,16 +770,30 @@ impl HandlerInner {
fn print_error_count(&mut self, registry: &Registry) {
self.emit_stashed_diagnostics();

let s = match self.deduplicated_err_count {
0 => return,
let warnings = match self.deduplicated_warn_count {
0 => String::new(),
1 => "1 warning emitted".to_string(),
count => format!("{} warnings emitted", count),
};
let errors = match self.deduplicated_err_count {
0 => String::new(),
1 => "aborting due to previous error".to_string(),
count => format!("aborting due to {} previous errors", count),
};
if self.treat_err_as_bug() {
return;
}

let _ = self.fatal(&s);
match (errors.len(), warnings.len()) {
(0, 0) => return,
(0, _) => self.emit_diagnostic(&Diagnostic::new(Level::Warning, &warnings)),
(_, 0) => {
let _ = self.fatal(&errors);
}
(_, _) => {
let _ = self.fatal(&format!("{}; {}", &errors, &warnings));
}
}

let can_show_explain = self.emitter.should_show_explain();
let are_there_diagnostics = !self.emitted_diagnostic_codes.is_empty();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
});
self.check_and_note_conflicting_crates(diag, terr);
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id);
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id.to_def_id());

// It reads better to have the error origin as the final
// thing.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
{
sess.time("match_checking", || {
tcx.par_body_owners(|def_id| {
tcx.ensure().check_match(def_id);
tcx.ensure().check_match(def_id.to_def_id());
});
});
},
Expand All @@ -834,7 +834,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
});

sess.time("MIR_borrow_checking", || {
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id.to_def_id()));
});

sess.time("dumping_chalk_like_clauses", || {
Expand All @@ -843,7 +843,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {

sess.time("MIR_effect_checking", || {
for def_id in tcx.body_owners() {
mir::transform::check_unsafety::check_unsafety(tcx, def_id)
mir::transform::check_unsafety::check_unsafety(tcx, def_id.to_def_id())
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ declare_lint_pass!(
);

fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
// trigger the query once for all constants since that will already report the errors
// FIXME: Use ensure here
let _ = cx.tcx.const_eval_poly(def_id);
Expand Down
37 changes: 18 additions & 19 deletions src/librustc_middle/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,15 @@ impl<'hir> Map<'hir> {
}

pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id.expect_local()))
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
}

pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
self.tcx.definitions.def_path(def_id)
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
#[inline]
pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId {
pub fn local_def_id_from_node_id(&self, node: NodeId) -> LocalDefId {
self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| {
let hir_id = self.node_id_to_hir_id(node);
bug!(
Expand All @@ -173,24 +172,26 @@ impl<'hir> Map<'hir> {
// FIXME(eddyb) this function can and should return `LocalDefId`.
#[inline]
pub fn local_def_id(&self, hir_id: HirId) -> DefId {
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
bug!(
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
hir_id,
self.find_entry(hir_id)
)
})
self.opt_local_def_id(hir_id)
.unwrap_or_else(|| {
bug!(
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
hir_id,
self.find_entry(hir_id)
)
})
.to_def_id()
}

#[inline]
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
let node_id = self.hir_id_to_node_id(hir_id);
self.opt_local_def_id_from_node_id(node_id)
}

#[inline]
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<DefId> {
Some(self.tcx.definitions.opt_local_def_id(node)?.to_def_id())
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<LocalDefId> {
self.tcx.definitions.opt_local_def_id(node)
}

#[inline]
Expand Down Expand Up @@ -366,9 +367,8 @@ impl<'hir> Map<'hir> {
parent
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
self.local_def_id(self.body_owner(id))
pub fn body_owner_def_id(&self, id: BodyId) -> LocalDefId {
self.local_def_id(self.body_owner(id)).expect_local()
}

/// Given a `HirId`, returns the `BodyId` associated with it,
Expand Down Expand Up @@ -720,9 +720,8 @@ impl<'hir> Map<'hir> {
scope
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
pub fn get_parent_did(&self, id: HirId) -> DefId {
self.local_def_id(self.get_parent_item(id))
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
self.local_def_id(self.get_parent_item(id)).expect_local()
}

pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
Expand Down
15 changes: 11 additions & 4 deletions src/librustc_middle/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::cmp;
use std::fmt;
use std::iter;
use std::mem;
use std::num::NonZeroUsize;
use std::ops::Bound;

pub trait IntegerExt {
Expand Down Expand Up @@ -518,7 +519,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
// The never type.
ty::Never => tcx.intern_layout(Layout {
variants: Variants::Single { index: VariantIdx::new(0) },
fields: FieldsShape::Union(0),
fields: FieldsShape::Primitive,
abi: Abi::Uninhabited,
largest_niche: None,
align: dl.i8_align,
Expand Down Expand Up @@ -744,7 +745,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {

return Ok(tcx.intern_layout(Layout {
variants: Variants::Single { index },
fields: FieldsShape::Union(variants[index].len()),
fields: FieldsShape::Union(
NonZeroUsize::new(variants[index].len())
.ok_or(LayoutError::Unknown(ty))?,
),
abi,
largest_niche: None,
align,
Expand Down Expand Up @@ -1988,7 +1992,7 @@ where
if index == variant_index &&
// Don't confuse variants of uninhabited enums with the enum itself.
// For more details see https://github.com/rust-lang/rust/issues/69763.
this.fields != FieldsShape::Union(0) =>
this.fields != FieldsShape::Primitive =>
{
this.layout
}
Expand All @@ -2006,7 +2010,10 @@ where
let tcx = cx.tcx();
tcx.intern_layout(Layout {
variants: Variants::Single { index: variant_index },
fields: FieldsShape::Union(fields),
fields: match NonZeroUsize::new(fields) {
Some(fields) => FieldsShape::Union(fields),
None => FieldsShape::Arbitrary { offsets: vec![], memory_index: vec![] },
},
abi: Abi::Uninhabited,
largest_niche: None,
align: tcx.data_layout.i8_align,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2657,21 +2657,21 @@ pub enum ImplOverlapKind {

impl<'tcx> TyCtxt<'tcx> {
pub fn body_tables(self, body: hir::BodyId) -> &'tcx TypeckTables<'tcx> {
self.typeck_tables_of(self.hir().body_owner_def_id(body))
self.typeck_tables_of(self.hir().body_owner_def_id(body).to_def_id())
}

/// Returns an iterator of the `DefId`s for all body-owners in this
/// crate. If you would prefer to iterate over the bodies
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
pub fn body_owners(self) -> impl Iterator<Item = DefId> + Captures<'tcx> + 'tcx {
pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + Captures<'tcx> + 'tcx {
self.hir()
.krate()
.body_ids
.iter()
.map(move |&body_id| self.hir().body_owner_def_id(body_id))
}

pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) {
pub fn par_body_owners<F: Fn(LocalDefId) + sync::Sync + sync::Send>(self, f: F) {
par_iter(&self.hir().krate().body_ids)
.for_each(|&body_id| f(self.hir().body_owner_def_id(body_id)));
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {

pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
let parent_id = tcx.hir().get_parent_did(hir_id);
if !parent_id.is_top_level_module() {
is_const_impl_raw(tcx, parent_id.expect_local())
} else {
false
}
if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }
}

/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
Expand Down
8 changes: 8 additions & 0 deletions src/librustc_mir/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ pub trait Machine<'mir, 'tcx>: Sized {
kind: Option<MemoryKind<Self::MemoryKind>>,
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);

/// Called to notify the machine before a deallocation occurs.
fn before_deallocation(
_memory_extra: &mut Self::MemoryExtra,
_id: AllocId,
) -> InterpResult<'tcx> {
Ok(())
}

/// Return the "base" tag for the given *global* allocation: the one that is used for direct
/// accesses to this static/const/fn allocation. If `id` is not a global allocation,
/// this will return an unusable tag (i.e., accesses will be UB)!
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
);
}

M::before_deallocation(&mut self.extra, ptr.alloc_id)?;

let (alloc_kind, mut alloc) = match self.alloc_map.remove(&ptr.alloc_id) {
Some(alloc) => alloc,
None => {
Expand Down
10 changes: 6 additions & 4 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::convert::TryFrom;
use std::fmt::Write;
use std::num::NonZeroUsize;
use std::ops::RangeInclusive;

use rustc_data_structures::fx::FxHashSet;
Expand Down Expand Up @@ -642,10 +643,11 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
}

#[inline(always)]
fn visit_union(&mut self, op: OpTy<'tcx, M::PointerTag>, fields: usize) -> InterpResult<'tcx> {
// Empty unions are not accepted by rustc. But uninhabited enums
// claim to be unions, so allow them, too.
assert!(op.layout.abi.is_uninhabited() || fields > 0);
fn visit_union(
&mut self,
_op: OpTy<'tcx, M::PointerTag>,
_fields: NonZeroUsize,
) -> InterpResult<'tcx> {
Ok(())
}

Expand Down
5 changes: 4 additions & 1 deletion src/librustc_mir/interpret/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use rustc_middle::ty;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_target::abi::{FieldsShape, VariantIdx, Variants};

use std::num::NonZeroUsize;

use super::{InterpCx, MPlaceTy, Machine, OpTy};

// A thing that we can project into, and that has a layout.
Expand Down Expand Up @@ -130,7 +132,7 @@ macro_rules! make_value_visitor {
}
/// Visits the given value as a union. No automatic recursion can happen here.
#[inline(always)]
fn visit_union(&mut self, _v: Self::V, _fields: usize) -> InterpResult<'tcx>
fn visit_union(&mut self, _v: Self::V, _fields: NonZeroUsize) -> InterpResult<'tcx>
{
Ok(())
}
Expand Down Expand Up @@ -208,6 +210,7 @@ macro_rules! make_value_visitor {

// Visit the fields of this value.
match v.layout().fields {
FieldsShape::Primitive => {},
FieldsShape::Union(fields) => {
self.visit_union(v, fields)?;
},
Expand Down
Loading