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 5 pull requests #67704

Closed
wants to merge 13 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'tcx> TyCtxt<'tcx> {
let substs = InternalSubsts::identity_for_item(self, def_id);
let instance = ty::Instance::new(def_id, substs);
let cid = GlobalId { instance, promoted: None };
let param_env = self.param_env(def_id);
let param_env = self.param_env(def_id).with_reveal_all();
self.const_eval_validated(param_env.and(cid))
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,9 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
// In either case, we handle this by not adding a
// candidate for an impl if it contains a `default`
// type.
//
// NOTE: This should be kept in sync with the similar code in
// `rustc::ty::instance::resolve_associated_item()`.
let node_item =
assoc_ty_def(selcx, impl_data.impl_def_id, obligation.predicate.item_def_id);

Expand Down
19 changes: 19 additions & 0 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,25 @@ fn resolve_associated_item<'tcx>(
traits::VtableImpl(impl_data) => {
let (def_id, substs) =
traits::find_associated_item(tcx, param_env, trait_item, rcvr_substs, &impl_data);

let resolved_item = tcx.associated_item(def_id);

// Since this is a trait item, we need to see if the item is either a trait default item
// or a specialization because we can't resolve those unless we can `Reveal::All`.
// NOTE: This should be kept in sync with the similar code in
// `rustc::traits::project::assemble_candidates_from_impls()`.
let eligible = if !resolved_item.defaultness.is_default() {
true
} else if param_env.reveal == traits::Reveal::All {
!trait_ref.needs_subst()
} else {
false
};

if !eligible {
return None;
}

let substs = tcx.erase_regions(&substs);
Some(ty::Instance::new(def_id, substs))
}
Expand Down
22 changes: 20 additions & 2 deletions src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,9 +882,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
drop_span,
format!(
"...but `{}` will be dropped here, when the function `{}` returns",
"...but `{}` will be dropped here, when the {} returns",
name,
self.infcx.tcx.hir().name(fn_hir_id),
self.infcx
.tcx
.hir()
.opt_name(fn_hir_id)
.map(|name| format!("function `{}`", name))
.unwrap_or_else(|| {
match &self
.infcx
.tcx
.typeck_tables_of(self.mir_def_id)
.node_type(fn_hir_id)
.kind
{
ty::Closure(..) => "enclosing closure",
ty::Generator(..) => "enclosing generator",
kind => bug!("expected closure or generator, found {:?}", kind),
}
.to_string()
})
),
);

Expand Down
22 changes: 13 additions & 9 deletions src/librustc_mir/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ pub fn const_eval_validated_provider<'tcx>(
key.param_env.reveal = Reveal::UserFacing;
match tcx.const_eval_validated(key) {
// try again with reveal all as requested
Err(ErrorHandled::TooGeneric) => {
// Promoteds should never be "too generic" when getting evaluated.
// They either don't get evaluated, or we are in a monomorphic context
assert!(key.value.promoted.is_none());
}
Err(ErrorHandled::TooGeneric) => {}
// dedupliate calls
other => return other,
}
Expand Down Expand Up @@ -301,10 +297,18 @@ pub fn const_eval_raw_provider<'tcx>(
// Ensure that if the above error was either `TooGeneric` or `Reported`
// an error must be reported.
let v = err.report_as_error(ecx.tcx, "could not evaluate static initializer");
tcx.sess.delay_span_bug(
err.span,
&format!("static eval failure did not emit an error: {:#?}", v),
);

// If this is `Reveal:All`, then we need to make sure an error is reported but if
// this is `Reveal::UserFacing`, then it's expected that we could get a
// `TooGeneric` error. When we fall back to `Reveal::All`, then it will either
// succeed or we'll report this error then.
if key.param_env.reveal == Reveal::All {
tcx.sess.delay_span_bug(
err.span,
&format!("static eval failure did not emit an error: {:#?}", v),
);
}

v
} else if def_id.is_local() {
// constant defined in this crate, we can figure out a lint level!
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let kind = match res {
Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
let substs = self.tables.node_substs(id);
match self.tcx.const_eval_resolve(self.param_env, def_id, substs, Some(span)) {
// Use `Reveal::All` here because patterns are always monomorphic even if their function isn't.
match self.tcx.const_eval_resolve(
self.param_env.with_reveal_all(),
def_id,
substs,
Some(span),
) {
Ok(value) => {
let pattern = self.const_to_pat(value, id, span);
if !is_associated_const {
Expand Down
15 changes: 12 additions & 3 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,19 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
}
ty::Array(tys, ..) | ty::Slice(tys)
if {
// This optimization applies only for integer and floating point types
// (i.e., types that can hold arbitrary bytes).
// This optimization applies for types that can hold arbitrary bytes (such as
// integer and floating point types) or for structs or tuples with no fields.
// FIXME(wesleywiser) This logic could be extended further to arbitrary structs
// or tuples made up of integer/floating point types or inhabited ZSTs with no
// padding.
match tys.kind {
ty::Int(..) | ty::Uint(..) | ty::Float(..) => true,
ty::Tuple(tys) if tys.len() == 0 => true,
ty::Adt(adt_def, _)
if adt_def.is_struct() && adt_def.all_fields().next().is_none() =>
{
true
}
_ => false,
}
} =>
Expand All @@ -609,7 +618,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
// Size is not 0, get a pointer.
let ptr = self.ecx.force_ptr(mplace.ptr)?;

// This is the optimization: we just check the entire range at once.
// Optimization: we just check the entire range at once.
// NOTE: Keep this in sync with the handling of integer and float
// types above, in `visit_primitive`.
// In run-time mode, we accept pointers in here. This is actually more
Expand Down
71 changes: 52 additions & 19 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::cell::Cell;

use rustc::hir::def::DefKind;
use rustc::hir::def_id::DefId;
use rustc::hir::HirId;
use rustc::mir::interpret::{InterpResult, PanicInfo, Scalar};
use rustc::mir::visit::{
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
Expand All @@ -20,7 +21,7 @@ use rustc::ty::layout::{
HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
};
use rustc::ty::subst::InternalSubsts;
use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
use rustc_data_structures::fx::FxHashMap;
use rustc_index::vec::IndexVec;
use syntax::ast::Mutability;
Expand Down Expand Up @@ -260,6 +261,9 @@ struct ConstPropagator<'mir, 'tcx> {
source_scopes: IndexVec<SourceScope, SourceScopeData>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
ret: Option<OpTy<'tcx, ()>>,
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
// the last known `SourceInfo` here and just keep revisiting it.
source_info: Option<SourceInfo>,
}

impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
Expand Down Expand Up @@ -293,13 +297,20 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
source: MirSource<'tcx>,
) -> ConstPropagator<'mir, 'tcx> {
let def_id = source.def_id();
let param_env = tcx.param_env(def_id);
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
let mut param_env = tcx.param_env(def_id);

// If we're evaluating inside a monomorphic function, then use `Reveal::All` because
// we want to see the same instances that codegen will see. This allows us to `resolve()`
// specializations.
if !substs.needs_subst() {
param_env = param_env.with_reveal_all();
}

let span = tcx.def_span(def_id);
let mut ecx = InterpCx::new(tcx.at(span), param_env, ConstPropMachine, ());
let can_const_prop = CanConstProp::check(body);

let substs = &InternalSubsts::identity_for_item(tcx, def_id);

let ret = ecx
.layout_of(body.return_ty().subst(tcx, substs))
.ok()
Expand Down Expand Up @@ -331,6 +342,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
local_decls: body.local_decls.clone(),
ret: ret.map(Into::into),
source_info: None,
}
}

Expand All @@ -352,6 +364,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
LocalState { value: LocalValue::Uninitialized, layout: Cell::new(None) };
}

fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
match &self.source_scopes[source_info.scope].local_data {
ClearCrossCrate::Set(data) => Some(data.lint_root),
ClearCrossCrate::Clear => None,
}
}

fn use_ecx<F, T>(&mut self, source_info: SourceInfo, f: F) -> Option<T>
where
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
Expand All @@ -360,10 +379,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
// FIXME(eddyb) move this to the `Panic(_)` error case, so that
// `f(self)` is always called, and that the only difference when the
// scope's `local_data` is missing, is that the lint isn't emitted.
let lint_root = match &self.source_scopes[source_info.scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root,
ClearCrossCrate::Clear => return None,
};
let lint_root = self.lint_root(source_info)?;
let r = match f(self) {
Ok(val) => Some(val),
Err(error) => {
Expand Down Expand Up @@ -409,13 +425,31 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
r
}

fn eval_constant(&mut self, c: &Constant<'tcx>) -> Option<Const<'tcx>> {
fn eval_constant(
&mut self,
c: &Constant<'tcx>,
source_info: SourceInfo,
) -> Option<Const<'tcx>> {
self.ecx.tcx.span = c.span;
match self.ecx.eval_const_to_op(c.literal, None) {
Ok(op) => Some(op),
Err(error) => {
let err = error_to_const_error(&self.ecx, error);
err.report_as_error(self.ecx.tcx, "erroneous constant used");
match self.lint_root(source_info) {
Some(lint_root) if c.literal.needs_subst() => {
// Out of backwards compatibility we cannot report hard errors in unused
// generic functions using associated constants of the generic parameters.
err.report_as_lint(
self.ecx.tcx,
"erroneous constant used",
lint_root,
Some(c.span),
);
}
_ => {
err.report_as_error(self.ecx.tcx, "erroneous constant used");
}
}
None
}
}
Expand All @@ -428,7 +462,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {

fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
match *op {
Operand::Constant(ref c) => self.eval_constant(c),
Operand::Constant(ref c) => self.eval_constant(c, source_info),
Operand::Move(ref place) | Operand::Copy(ref place) => {
self.eval_place(place, source_info)
}
Expand Down Expand Up @@ -495,10 +529,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
let right_size = r.layout.size;
let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
if r_bits.map_or(false, |b| b >= left_bits as u128) {
let lint_root = match &self.source_scopes[source_info.scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root,
ClearCrossCrate::Clear => return None,
};
let lint_root = self.lint_root(source_info)?;
let dir = if *op == BinOp::Shr { "right" } else { "left" };
self.tcx.lint_hir(
::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
Expand Down Expand Up @@ -748,18 +779,19 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
trace!("visit_constant: {:?}", constant);
self.super_constant(constant, location);
self.eval_constant(constant);
self.eval_constant(constant, self.source_info.unwrap());
}

fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
trace!("visit_statement: {:?}", statement);
let source_info = statement.source_info;
self.source_info = Some(source_info);
if let StatementKind::Assign(box (ref place, ref mut rval)) = statement.kind {
let place_ty: Ty<'tcx> = place.ty(&self.local_decls, self.tcx).ty;
if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
if let Some(local) = place.as_local() {
let source = statement.source_info;
let can_const_prop = self.can_const_prop[local];
if let Some(()) = self.const_prop(rval, place_layout, source, place) {
if let Some(()) = self.const_prop(rval, place_layout, source_info, place) {
if can_const_prop == ConstPropMode::FullConstProp
|| can_const_prop == ConstPropMode::OnlyPropagateInto
{
Expand Down Expand Up @@ -802,8 +834,9 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
}

fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
self.super_terminator(terminator, location);
let source_info = terminator.source_info;
self.source_info = Some(source_info);
self.super_terminator(terminator, location);
match &mut terminator.kind {
TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
if let Some(value) = self.eval_operand(&cond, source_info) {
Expand Down
13 changes: 10 additions & 3 deletions src/librustc_mir/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use rustc_index::vec::{Idx, IndexVec};

use rustc::mir::visit::*;
use rustc::mir::*;
use rustc::ty::subst::{Subst, SubstsRef};
use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
use rustc::ty::subst::{InternalSubsts, Subst, SubstsRef};
use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt, TypeFoldable};

use super::simplify::{remove_dead_blocks, CfgSimplifier};
use crate::transform::{MirPass, MirSource};
Expand Down Expand Up @@ -66,7 +66,14 @@ impl Inliner<'tcx> {

let mut callsites = VecDeque::new();

let param_env = self.tcx.param_env(self.source.def_id());
let mut param_env = self.tcx.param_env(self.source.def_id());

let substs = &InternalSubsts::identity_for_item(self.tcx, self.source.def_id());

// For monomorphic functions, we can use `Reveal::All` to resolve specialized instances.
if !substs.needs_subst() {
param_env = param_env.with_reveal_all();
}

// Only do inlining into fn bodies.
let id = self.tcx.hir().as_local_hir_id(self.source.def_id()).unwrap();
Expand Down
26 changes: 1 addition & 25 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,31 +528,7 @@ impl<'a> Resolver<'a> {
resolution.shadowed_glob = Some(glob_binding);
}
(false, false) => {
if let (&NameBindingKind::Res(_, true), &NameBindingKind::Res(_, true)) =
(&old_binding.kind, &binding.kind)
{
this.session
.struct_span_err(
binding.span,
&format!(
"a macro named `{}` has already been exported",
key.ident
),
)
.span_label(
binding.span,
format!("`{}` already exported", key.ident),
)
.span_note(
old_binding.span,
"previous macro export is now shadowed",
)
.emit();

resolution.binding = Some(binding);
} else {
return Err(old_binding);
}
return Err(old_binding);
}
}
} else {
Expand Down
Loading