Skip to content

Commit bb59453

Browse files
committed
Auto merge of #121345 - Nilstrieb:rollup-reb0xge, r=Nilstrieb
Rollup of 8 pull requests Successful merges: - #121167 (resolve: Scale back unloading of speculatively loaded crates) - #121196 (Always inline check in `assert_unsafe_precondition` with cfg(debug_assertions)) - #121241 (Implement `NonZero` traits generically.) - #121278 (Remove the "codegen" profile from bootstrap) - #121286 (Rename `ConstPropLint` to `KnownPanicsLint`) - #121291 (target: Revert default to the medium code model on LoongArch targets) - #121302 (Remove `RefMutL` hack in `proc_macro::bridge`) - #121318 (Trigger `unsafe_code` lint on invocations of `global_asm`) Failed merges: - #121206 (Top level error handling) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2b43e75 + d61adbf commit bb59453

File tree

32 files changed

+355
-302
lines changed

32 files changed

+355
-302
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ pub(super) fn expand_global_asm<'cx>(
773773
kind: ast::VisibilityKind::Inherited,
774774
tokens: None,
775775
},
776-
span: ecx.with_def_site_ctxt(sp),
776+
span: sp,
777777
tokens: None,
778778
})])
779779
} else {

compiler/rustc_lint/messages.ftl

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ lint_builtin_explicit_outlives = outlives requirements can be inferred
7272
7373
lint_builtin_export_name_fn = declaration of a function with `export_name`
7474
lint_builtin_export_name_method = declaration of a method with `export_name`
75-
7675
lint_builtin_export_name_static = declaration of a static with `export_name`
76+
77+
lint_builtin_global_asm = usage of `core::arch::global_asm`
78+
lint_builtin_global_macro_unsafety = using this macro is unsafe even though it does not need an `unsafe` block
79+
7780
lint_builtin_impl_unsafe_method = implementation of an `unsafe` method
7881
7982
lint_builtin_incomplete_features = the feature `{$name}` is incomplete and may not be safe to use and/or cause compiler crashes

compiler/rustc_lint/src/builtin.rs

+4
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ impl EarlyLintPass for UnsafeCode {
393393
}
394394
}
395395

396+
ast::ItemKind::GlobalAsm(..) => {
397+
self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm);
398+
}
399+
396400
_ => {}
397401
}
398402
}

compiler/rustc_lint/src/lints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ pub enum BuiltinUnsafe {
114114
DeclUnsafeMethod,
115115
#[diag(lint_builtin_impl_unsafe_method)]
116116
ImplUnsafeMethod,
117+
#[diag(lint_builtin_global_asm)]
118+
#[note(lint_builtin_global_macro_unsafety)]
119+
GlobalAsm,
117120
}
118121

119122
#[derive(LintDiagnostic)]

compiler/rustc_metadata/src/creader.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1070,16 +1070,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
10701070
pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> {
10711071
self.maybe_resolve_crate(name, CrateDepKind::Explicit, None).ok()
10721072
}
1073-
1074-
pub fn unload_unused_crates(&mut self) {
1075-
for opt_cdata in &mut self.cstore.metas {
1076-
if let Some(cdata) = opt_cdata
1077-
&& !cdata.used()
1078-
{
1079-
*opt_cdata = None;
1080-
}
1081-
}
1082-
}
10831073
}
10841074

10851075
fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+10
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,16 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
519519
tcx.untracked().cstore.freeze();
520520
tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
521521
},
522+
used_crates: |tcx, ()| {
523+
// The list of loaded crates is now frozen in query cache,
524+
// so make sure cstore is not mutably accessed from here on.
525+
tcx.untracked().cstore.freeze();
526+
tcx.arena.alloc_from_iter(
527+
CStore::from_tcx(tcx)
528+
.iter_crate_data()
529+
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
530+
)
531+
},
522532
..providers.queries
523533
};
524534
provide_extern(&mut providers.extern_queries);

compiler/rustc_middle/src/query/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,13 @@ rustc_queries! {
18721872
eval_always
18731873
desc { "fetching all foreign CrateNum instances" }
18741874
}
1875+
// Crates that are loaded non-speculatively (not for diagnostics or doc links).
1876+
// FIXME: This is currently only used for collecting lang items, but should be used instead of
1877+
// `crates` in most other cases too.
1878+
query used_crates(_: ()) -> &'tcx [CrateNum] {
1879+
eval_always
1880+
desc { "fetching `CrateNum`s for all crates loaded non-speculatively" }
1881+
}
18751882

18761883
/// A list of all traits in a crate, used by rustdoc and error reporting.
18771884
query traits(_: CrateNum) -> &'tcx [DefId] {

compiler/rustc_mir_transform/src/const_prop_lint.rs compiler/rustc_mir_transform/src/known_panics_lint.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
//! Propagates constants for early reporting of statically known
2-
//! assertion failures
1+
//! A lint that checks for known panics like
2+
//! overflows, division by zero,
3+
//! out-of-bound access etc.
4+
//! Uses const propagation to determine the
5+
//! values of operands during checks.
36
47
use std::fmt::Debug;
58

@@ -21,9 +24,9 @@ use crate::dataflow_const_prop::DummyMachine;
2124
use crate::errors::{AssertLint, AssertLintKind};
2225
use crate::MirLint;
2326

24-
pub struct ConstPropLint;
27+
pub struct KnownPanicsLint;
2528

26-
impl<'tcx> MirLint<'tcx> for ConstPropLint {
29+
impl<'tcx> MirLint<'tcx> for KnownPanicsLint {
2730
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
2831
if body.tainted_by_errors.is_some() {
2932
return;
@@ -37,31 +40,28 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
3740
// Only run const prop on functions, methods, closures and associated constants
3841
if !is_fn_like && !is_assoc_const {
3942
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
40-
trace!("ConstPropLint skipped for {:?}", def_id);
43+
trace!("KnownPanicsLint skipped for {:?}", def_id);
4144
return;
4245
}
4346

4447
// FIXME(welseywiser) const prop doesn't work on coroutines because of query cycles
4548
// computing their layout.
4649
if tcx.is_coroutine(def_id.to_def_id()) {
47-
trace!("ConstPropLint skipped for coroutine {:?}", def_id);
50+
trace!("KnownPanicsLint skipped for coroutine {:?}", def_id);
4851
return;
4952
}
5053

51-
trace!("ConstPropLint starting for {:?}", def_id);
54+
trace!("KnownPanicsLint starting for {:?}", def_id);
5255

53-
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
54-
// constants, instead of just checking for const-folding succeeding.
55-
// That would require a uniform one-def no-mutation analysis
56-
// and RPO (or recursing when needing the value of a local).
5756
let mut linter = ConstPropagator::new(body, tcx);
5857
linter.visit_body(body);
5958

60-
trace!("ConstPropLint done for {:?}", def_id);
59+
trace!("KnownPanicsLint done for {:?}", def_id);
6160
}
6261
}
6362

64-
/// Finds optimization opportunities on the MIR.
63+
/// Visits MIR nodes, performs const propagation
64+
/// and runs lint checks as it goes
6565
struct ConstPropagator<'mir, 'tcx> {
6666
ecx: InterpCx<'mir, 'tcx, DummyMachine>,
6767
tcx: TyCtxt<'tcx>,
@@ -238,7 +238,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
238238
// dedicated error variants should be introduced instead.
239239
assert!(
240240
!error.kind().formatted_string(),
241-
"const-prop encountered formatting error: {}",
241+
"known panics lint encountered formatting error: {}",
242242
format_interp_error(self.ecx.tcx.dcx(), error),
243243
);
244244
None
@@ -253,7 +253,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
253253
return None;
254254
}
255255

256-
// Normalization needed b/c const prop lint runs in
256+
// Normalization needed b/c known panics lint runs in
257257
// `mir_drops_elaborated_and_const_checked`, which happens before
258258
// optimized MIR. Only after optimizing the MIR can we guarantee
259259
// that the `RevealAll` pass has happened and that the body's consts
@@ -864,6 +864,8 @@ pub enum ConstPropMode {
864864
NoPropagation,
865865
}
866866

867+
/// A visitor that determines locals in a MIR body
868+
/// that can be const propagated
867869
pub struct CanConstProp {
868870
can_const_prop: IndexVec<Local, ConstPropMode>,
869871
// False at the beginning. Once set, no more assignments are allowed to that local.

compiler/rustc_mir_transform/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ mod remove_place_mention;
5959
mod add_subtyping_projections;
6060
pub mod cleanup_post_borrowck;
6161
mod const_debuginfo;
62-
mod const_prop_lint;
6362
mod copy_prop;
6463
mod coroutine;
6564
mod cost_checker;
@@ -83,6 +82,7 @@ mod gvn;
8382
pub mod inline;
8483
mod instsimplify;
8584
mod jump_threading;
85+
mod known_panics_lint;
8686
mod large_enums;
8787
mod lint;
8888
mod lower_intrinsics;
@@ -533,7 +533,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
533533
&elaborate_box_derefs::ElaborateBoxDerefs,
534534
&coroutine::StateTransform,
535535
&add_retag::AddRetag,
536-
&Lint(const_prop_lint::ConstPropLint),
536+
&Lint(known_panics_lint::KnownPanicsLint),
537537
];
538538
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
539539
}

compiler/rustc_passes/src/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
250250
let mut collector = LanguageItemCollector::new(tcx, resolver);
251251

252252
// Collect lang items in other crates.
253-
for &cnum in tcx.crates(()).iter() {
253+
for &cnum in tcx.used_crates(()).iter() {
254254
for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
255255
collector.collect_item(lang_item, def_id, None);
256256
}

compiler/rustc_resolve/src/late.rs

-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_hir::def::Namespace::{self, *};
2323
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
2424
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
2525
use rustc_hir::{PrimTy, TraitCandidate};
26-
use rustc_metadata::creader::CStore;
2726
use rustc_middle::middle::resolve_bound_vars::Set1;
2827
use rustc_middle::{bug, span_bug};
2928
use rustc_session::config::{CrateType, ResolveDocLinks};
@@ -4574,10 +4573,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
45744573
// Encoding foreign def ids in proc macro crate metadata will ICE.
45754574
return None;
45764575
}
4577-
// Doc paths should be resolved speculatively and should not produce any
4578-
// diagnostics, but if they are indeed resolved, then we need to keep the
4579-
// corresponding crate alive.
4580-
CStore::from_tcx_mut(self.r.tcx).set_used_recursively(def_id.krate);
45814576
}
45824577
res
45834578
});

compiler/rustc_resolve/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16511651
self.tcx
16521652
.sess
16531653
.time("resolve_postprocess", || self.crate_loader(|c| c.postprocess(krate)));
1654-
self.crate_loader(|c| c.unload_unused_crates());
16551654
});
16561655

16571656
// Make sure we don't mutate the cstore from here on.

compiler/rustc_target/src/spec/targets/loongarch64_unknown_linux_gnu.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{base, CodeModel, Target, TargetOptions};
1+
use crate::spec::{base, Target, TargetOptions};
22

33
pub fn target() -> Target {
44
Target {
@@ -7,7 +7,6 @@ pub fn target() -> Target {
77
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n64-S128".into(),
88
arch: "loongarch64".into(),
99
options: TargetOptions {
10-
code_model: Some(CodeModel::Medium),
1110
cpu: "generic".into(),
1211
features: "+f,+d".into(),
1312
llvm_abiname: "lp64d".into(),

compiler/rustc_target/src/spec/targets/loongarch64_unknown_none.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn target() -> Target {
1616
max_atomic_width: Some(64),
1717
relocation_model: RelocModel::Static,
1818
panic_strategy: PanicStrategy::Abort,
19-
code_model: Some(CodeModel::Medium),
19+
code_model: Some(CodeModel::Small),
2020
..Default::default()
2121
},
2222
}

compiler/rustc_target/src/spec/targets/loongarch64_unknown_none_softfloat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn target() -> Target {
1717
max_atomic_width: Some(64),
1818
relocation_model: RelocModel::Static,
1919
panic_strategy: PanicStrategy::Abort,
20-
code_model: Some(CodeModel::Medium),
20+
code_model: Some(CodeModel::Small),
2121
..Default::default()
2222
},
2323
}

library/core/src/intrinsics.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2575,6 +2575,7 @@ pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
25752575
/// assertions disabled. This intrinsic is primarily used by [`assert_unsafe_precondition`].
25762576
#[rustc_const_unstable(feature = "delayed_debug_assertions", issue = "none")]
25772577
#[unstable(feature = "core_intrinsics", issue = "none")]
2578+
#[inline(always)]
25782579
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
25792580
pub(crate) const fn debug_assertions() -> bool {
25802581
cfg!(debug_assertions)
@@ -2659,7 +2660,13 @@ pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize)
26592660
macro_rules! assert_unsafe_precondition {
26602661
($message:expr, ($($name:ident:$ty:ty = $arg:expr),*$(,)?) => $e:expr $(,)?) => {
26612662
{
2662-
#[inline(never)]
2663+
// When the standard library is compiled with debug assertions, we want the check to inline for better performance.
2664+
// This is important when working on the compiler, which is compiled with debug assertions locally.
2665+
// When not compiled with debug assertions (so the precompiled std) we outline the check to minimize the compile
2666+
// time impact when debug assertions are disabled.
2667+
// It is not clear whether that is the best solution, see #120848.
2668+
#[cfg_attr(debug_assertions, inline(always))]
2669+
#[cfg_attr(not(debug_assertions), inline(never))]
26632670
#[rustc_nounwind]
26642671
fn precondition_check($($name:$ty),*) {
26652672
if !$e {

0 commit comments

Comments
 (0)