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 12 pull requests #81394

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b43aa96
Print failure message on all tests that should panic, but don't
johanngan Jan 10, 2021
679f6f3
Add `unwrap_unchecked()` methods for `Option` and `Result`
ojeda Jan 10, 2021
76299b3
Add `SAFETY` annotations
ojeda Jan 10, 2021
63a1eee
Reset LateContext enclosing body in nested items
camsteffen Jan 18, 2021
21fb586
Query for TypeckResults in LateContext::qpath_res
camsteffen Jan 18, 2021
def0e9b
Fix ICE with `ReadPointerAsBytes` validation error
camelid Jan 11, 2021
a7b7a43
Move test to `src/test/ui/consts/`
camelid Jan 11, 2021
eaba3da
Remove qpath_res util function
camsteffen Jan 18, 2021
e25959b
Make more traits of the From/Into family diagnostic items
flip1995 Jan 22, 2021
48f9dbf
clean up some const error reporting around promoteds
RalfJung Jan 24, 2021
6d4e03a
codegen: assume constants cannot fail to evaluate
RalfJung Jan 24, 2021
2be1993
Ignore test on 32-bit architectures
camelid Jan 25, 2021
59195a2
rustc_codegen_ssa: use wall time for codegen_to_LLVM_IR time-passes e…
tgnottingham Jan 25, 2021
26b4baf
Point to span of upvar making closure FnMut
sledgehammervampire Jan 18, 2021
088c89d
Account for generics when suggesting bound
estebank Jan 19, 2021
042facb
Fix some bugs reported by eslint
GuillaumeGomez Jan 23, 2021
0140dac
Link the reference about undefined behavior
ojeda Jan 25, 2021
01250fc
Add tracking issue
ojeda Jan 25, 2021
1c0a52d
rustdoc: Document CommonMark extensions.
ehuss Jan 25, 2021
6880d11
Rollup merge of #80868 - johanngan:should-panic-msg-with-expected, r=…
jonas-schievink Jan 25, 2021
975c4bc
Rollup merge of #80876 - ojeda:option-result-unwrap_unchecked, r=m-ou-se
jonas-schievink Jan 25, 2021
34abe17
Rollup merge of #80900 - camelid:readpointerasbytes-ice, r=oli-obk
jonas-schievink Jan 25, 2021
79ddb67
Rollup merge of #81158 - 1000teslas:issue-80313-fix, r=Aaron1011
jonas-schievink Jan 25, 2021
3210a25
Rollup merge of #81176 - camsteffen:qpath-res, r=oli-obk
jonas-schievink Jan 25, 2021
c2b8cd7
Rollup merge of #81195 - estebank:suggest-bound-on-trait-with-params,…
jonas-schievink Jan 25, 2021
1cbd775
Rollup merge of #81277 - flip1995:from_diag_items, r=matthewjasper
jonas-schievink Jan 25, 2021
457b3da
Rollup merge of #81299 - GuillaumeGomez:fix-eslint-detected-bugs, r=N…
jonas-schievink Jan 25, 2021
06ef1bf
Rollup merge of #81327 - RalfJung:codegen-no-const-fail, r=oli-obk
jonas-schievink Jan 25, 2021
34ca9c5
Rollup merge of #81333 - RalfJung:const-err-simplify, r=oli-obk
jonas-schievink Jan 25, 2021
f308d77
Rollup merge of #81369 - tgnottingham:codegen-to-llvm-ir-wall-time, r…
jonas-schievink Jan 25, 2021
ba8379b
Rollup merge of #81389 - ehuss:rustdoc-cmark-extensions, r=GuillaumeG…
jonas-schievink Jan 25, 2021
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
8 changes: 3 additions & 5 deletions compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ pub(crate) fn codegen_constant<'tcx>(
{
Ok(const_val) => const_val,
Err(_) => {
if promoted.is_none() {
fx.tcx
.sess
.span_err(constant.span, "erroneous constant encountered");
}
fx.tcx
.sess
.span_err(constant.span, "erroneous constant encountered");
return crate::trap::trap_unreachable_ret_value(
fx,
fx.layout_of(const_.ty),
Expand Down
31 changes: 14 additions & 17 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{CachedModuleCodegen, CrateInfo, MemFlags, ModuleCodegen, ModuleKind}
use rustc_attr as attr;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::profiling::print_time_passes_entry;
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
use rustc_data_structures::sync::{par_iter, ParallelIterator};
use rustc_hir as hir;
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem;
Expand Down Expand Up @@ -554,8 +554,6 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
codegen_units
};

let total_codegen_time = Lock::new(Duration::new(0, 0));

// The non-parallel compiler can only translate codegen units to LLVM IR
// on a single thread, leading to a staircase effect where the N LLVM
// threads have to wait on the single codegen threads to generate work
Expand All @@ -578,23 +576,25 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
.collect();

// Compile the found CGUs in parallel.
par_iter(cgus)
let start_time = Instant::now();

let pre_compiled_cgus = par_iter(cgus)
.map(|(i, _)| {
let start_time = Instant::now();
let module = backend.compile_codegen_unit(tcx, codegen_units[i].name());
let mut time = total_codegen_time.lock();
*time += start_time.elapsed();
(i, module)
})
.collect()
.collect();

(pre_compiled_cgus, start_time.elapsed())
})
} else {
FxHashMap::default()
(FxHashMap::default(), Duration::new(0, 0))
}
};

let mut cgu_reuse = Vec::new();
let mut pre_compiled_cgus: Option<FxHashMap<usize, _>> = None;
let mut total_codegen_time = Duration::new(0, 0);

for (i, cgu) in codegen_units.iter().enumerate() {
ongoing_codegen.wait_for_signal_to_codegen_item();
Expand All @@ -607,7 +607,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
codegen_units.iter().map(|cgu| determine_cgu_reuse(tcx, &cgu)).collect()
});
// Pre compile some CGUs
pre_compiled_cgus = Some(pre_compile_cgus(&cgu_reuse));
let (compiled_cgus, codegen_time) = pre_compile_cgus(&cgu_reuse);
pre_compiled_cgus = Some(compiled_cgus);
total_codegen_time += codegen_time;
}

let cgu_reuse = cgu_reuse[i];
Expand All @@ -621,8 +623,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
} else {
let start_time = Instant::now();
let module = backend.compile_codegen_unit(tcx, cgu.name());
let mut time = total_codegen_time.lock();
*time += start_time.elapsed();
total_codegen_time += start_time.elapsed();
module
};
submit_codegened_module_to_llvm(
Expand Down Expand Up @@ -663,11 +664,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(

// Since the main thread is sometimes blocked during codegen, we keep track
// -Ztime-passes output manually.
print_time_passes_entry(
tcx.sess.time_passes(),
"codegen_to_LLVM_IR",
total_codegen_time.into_inner(),
);
print_time_passes_entry(tcx.sess.time_passes(), "codegen_to_LLVM_IR", total_codegen_time);

ongoing_codegen.check_for_errors(tcx.sess);

Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_codegen_ssa/src/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
.tcx()
.const_eval_resolve(ty::ParamEnv::reveal_all(), def, substs, promoted, None)
.map_err(|err| {
if promoted.is_none() {
self.cx
.tcx()
.sess
.span_err(constant.span, "erroneous constant encountered");
}
self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
err
}),
ty::ConstKind::Value(value) => Ok(value),
Expand Down
21 changes: 21 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

fx.per_local_var_debug_info = fx.compute_per_local_var_debug_info(&mut bx);

let mut all_consts_ok = true;
for const_ in &mir.required_consts {
if let Err(err) = fx.eval_mir_constant(const_) {
all_consts_ok = false;
match err {
// errored or at least linted
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {}
Expand All @@ -199,6 +201,25 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
}
}
}
if !all_consts_ok {
// There is no delay_span_bug here, we just have to rely on a hard error actually having
// been raised.
bx.abort();
// `abort` does not terminate the block, so we still need to generate
// an `unreachable` terminator after it.
bx.unreachable();
// We also have to delete all the other basic blocks again...
for bb in mir.basic_blocks().indices() {
if bb != mir::START_BLOCK {
unsafe {
bx.delete_basic_block(fx.blocks[bb]);
}
}
}
// FIXME: Can we somehow avoid all this by evaluating the consts before creating the basic
// blocks? The tricky part is doing that without duplicating `fx.eval_mir_constant`.
return;
}

let memory_locals = analyze::non_ssa_locals(&fx);

Expand Down
25 changes: 4 additions & 21 deletions compiler/rustc_codegen_ssa/src/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use crate::glue;
use crate::traits::*;
use crate::MemFlags;

use rustc_errors::ErrorReported;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled, Pointer, Scalar};
use rustc_middle::mir::interpret::{ConstValue, Pointer, Scalar};
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::Ty;
use rustc_target::abi::{Abi, Align, LayoutOf, Size};
Expand Down Expand Up @@ -439,25 +438,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}

mir::Operand::Constant(ref constant) => {
self.eval_mir_constant_to_operand(bx, constant).unwrap_or_else(|err| {
match err {
// errored or at least linted
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {}
ErrorHandled::TooGeneric => {
bug!("codegen encountered polymorphic constant")
}
}
// Allow RalfJ to sleep soundly knowing that even refactorings that remove
// the above error (or silence it under some conditions) will not cause UB.
bx.abort();
// We still have to return an operand but it doesn't matter,
// this code is unreachable.
let ty = self.monomorphize(constant.literal.ty);
let layout = bx.cx().layout_of(ty);
bx.load_operand(PlaceRef::new_sized(
bx.cx().const_undef(bx.cx().type_ptr_to(bx.cx().backend_type(layout))),
layout,
))
// This cannot fail because we checked all required_consts in advance.
self.eval_mir_constant_to_operand(bx, constant).unwrap_or_else(|_err| {
span_bug!(constant.span, "erroneous constant not captured by required_consts")
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,14 @@ impl<'tcx> LateContext<'tcx> {
hir::QPath::Resolved(_, ref path) => path.res,
hir::QPath::TypeRelative(..) | hir::QPath::LangItem(..) => self
.maybe_typeck_results()
.filter(|typeck_results| typeck_results.hir_owner == id.owner)
.or_else(|| {
if self.tcx.has_typeck_results(id.owner.to_def_id()) {
Some(self.tcx.typeck(id.owner))
} else {
None
}
})
.and_then(|typeck_results| typeck_results.type_dependent_def(id))
.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
let generics = self.context.generics.take();
self.context.generics = it.kind.generics();
let old_cached_typeck_results = self.context.cached_typeck_results.take();
let old_enclosing_body = self.context.enclosing_body.take();
self.with_lint_attrs(it.hir_id, &it.attrs, |cx| {
cx.with_param_env(it.hir_id, |cx| {
lint_callback!(cx, check_item, it);
hir_visit::walk_item(cx, it);
lint_callback!(cx, check_item_post, it);
});
});
self.context.enclosing_body = old_enclosing_body;
self.context.cached_typeck_results.set(old_cached_typeck_results);
self.context.generics = generics;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use rustc_hir as hir;
use rustc_hir::Node;
use rustc_index::vec::Idx;
use rustc_middle::mir::{self, ClearCrossCrate, Local, LocalDecl, LocalInfo, Location};
use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{
hir::place::PlaceBase,
mir::{self, ClearCrossCrate, Local, LocalDecl, LocalInfo, Location},
};
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;
Expand Down Expand Up @@ -241,6 +244,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
format!("mut {}", self.local_names[local].unwrap()),
Applicability::MachineApplicable,
);
let tcx = self.infcx.tcx;
if let ty::Closure(id, _) = the_place_err.ty(self.body, tcx).ty.kind() {
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
}
}

// Also suggest adding mut for upvars
Expand Down Expand Up @@ -271,6 +278,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
}
}

let tcx = self.infcx.tcx;
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
{
if let ty::Closure(id, _) = ty.kind() {
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
}
}
}

// complete hack to approximate old AST-borrowck
Expand Down Expand Up @@ -463,6 +478,45 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
err.buffer(&mut self.errors_buffer);
}

// point to span of upvar making closure call require mutable borrow
fn show_mutating_upvar(
&self,
tcx: TyCtxt<'_>,
id: &hir::def_id::DefId,
the_place_err: PlaceRef<'tcx>,
err: &mut DiagnosticBuilder<'_>,
) {
let id = id.expect_local();
let tables = tcx.typeck(id);
let hir_id = tcx.hir().local_def_id_to_hir_id(id);
let (span, place) = &tables.closure_kind_origins()[hir_id];
let reason = if let PlaceBase::Upvar(upvar_id) = place.base {
let upvar = ty::place_to_string_for_capture(tcx, place);
match tables.upvar_capture(upvar_id) {
ty::UpvarCapture::ByRef(ty::UpvarBorrow {
kind: ty::BorrowKind::MutBorrow,
..
}) => {
format!("mutable borrow of `{}`", upvar)
}
ty::UpvarCapture::ByValue(_) => {
format!("possible mutation of `{}`", upvar)
}
_ => bug!("upvar `{}` borrowed, but not mutably", upvar),
}
} else {
bug!("not an upvar")
};
err.span_label(
*span,
format!(
"calling `{}` requires mutable binding due to {}",
self.describe_place(the_place_err).unwrap(),
reason
),
);
}

/// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
fn expected_fn_found_fn_mut_call(&self, err: &mut DiagnosticBuilder<'_>, sp: Span, act: &str) {
err.span_label(sp, format!("cannot {}", act));
Expand Down
Loading