Skip to content
Merged

Rustup #4730

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f66cdb3
Stabilize `unchecked_neg` and `unchecked_shifts`
nxsaken Nov 19, 2025
e887771
Auto merge of #149200 - yaahc:helper-compat-test, r=petrochenkov
bors Nov 25, 2025
c0e2329
Auto merge of #149148 - davidtwco:v0-mangling-on-nightly-std, r=Kobzol
bors Nov 25, 2025
76e55a4
Auto merge of #149140 - oli-obk:more-encoder-minimization, r=jdonszel…
bors Nov 26, 2025
66f99a2
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
bors Nov 26, 2025
637b6ac
Use cg_llvm's target_config in miri
bjorn3 Nov 25, 2025
b770d93
simplify and explain MiriBeRustCompilerCalls logic for crate types
RalfJung Nov 27, 2025
ff39c40
Improve test
bjorn3 Nov 27, 2025
a397d44
Rollup merge of #149087 - nxsaken:unchecked_neg_shifts_stabilize, r=A…
Zalathar Nov 28, 2025
7ec2b22
Rollup merge of #149107 - Enselic:option-inspect-mutation, r=jieyouxu
Zalathar Nov 28, 2025
3c398f7
Rollup merge of #149323 - bjorn3:miri_llvm_target_config, r=RalfJung
Zalathar Nov 28, 2025
2e5a09f
Rollup merge of #149380 - JonathanBrouwer:cfg_select_lints, r=Urgau
Zalathar Nov 28, 2025
5dfcb7d
Rollup merge of #149394 - Aditya-PS-05:test-issue-146445-guard-patter…
Zalathar Nov 28, 2025
7c1fca9
Auto merge of #149410 - Zalathar:rollup-wke6axp, r=Zalathar
bors Nov 28, 2025
cf5b7da
Prepare for merging from rust-lang/rust
RalfJung Nov 28, 2025
2b07606
Merge ref '88bd39beb342' from rust-lang/rust
RalfJung Nov 28, 2025
4f65c5d
test that target features given in the target spec are enabled
RalfJung Nov 27, 2025
4868afb
clippy
RalfJung Nov 28, 2025
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 rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c871d09d1cc32a649f4c5177bb819646260ed120
88bd39beb342e2ae7ded1eb7d58697416686d679
160 changes: 98 additions & 62 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

// The rustc crates we need
extern crate rustc_abi;
extern crate rustc_codegen_ssa;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_hir;
Expand All @@ -19,6 +20,7 @@ extern crate rustc_log;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;

/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this.
Expand All @@ -41,19 +43,22 @@ use std::num::{NonZero, NonZeroI32};
use std::ops::Range;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Once;
use std::sync::atomic::{AtomicU32, Ordering};

use miri::{
BacktraceStyle, BorrowTrackerMethod, GenmcConfig, GenmcCtx, MiriConfig, MiriEntryFnType,
ProvenanceMode, TreeBorrowsParams, ValidationMode, run_genmc_mode,
};
use rustc_abi::ExternAbi;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::sync::{self, DynSync};
use rustc_driver::Compilation;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::{self as hir, Node};
use rustc_hir_analysis::check::check_function_signature;
use rustc_interface::interface::Config;
use rustc_interface::util::DummyCodegenBackend;
use rustc_log::tracing::debug;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::{
Expand All @@ -63,8 +68,9 @@ use rustc_middle::query::LocalCrate;
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::EarlyDiagCtxt;
use rustc_session::config::{CrateType, ErrorOutputType, OptLevel};
use rustc_session::config::{CrateType, ErrorOutputType, OptLevel, Options};
use rustc_span::def_id::DefId;
use rustc_target::spec::Target;

use crate::log::setup::{deinit_loggers, init_early_loggers, init_late_loggers};

Expand Down Expand Up @@ -166,7 +172,31 @@ fn run_many_seeds(
}
}

/// Generates the codegen backend for code that Miri will interpret: we basically
/// use the dummy backend, except that we put the LLVM backend in charge of
/// target features.
fn make_miri_codegen_backend(opts: &Options, target: &Target) -> Box<dyn CodegenBackend> {
let early_dcx = EarlyDiagCtxt::new(opts.error_format);

// Use the target_config method of the default codegen backend (eg LLVM) to ensure the
// calculated target features match said backend by respecting eg -Ctarget-cpu.
let target_config_backend =
rustc_interface::util::get_codegen_backend(&early_dcx, &opts.sysroot, None, target);
let target_config_backend_init = Once::new();

Box::new(DummyCodegenBackend {
target_config_override: Some(Box::new(move |sess| {
target_config_backend_init.call_once(|| target_config_backend.init(sess));
target_config_backend.target_config(sess)
})),
})
}

impl rustc_driver::Callbacks for MiriCompilerCalls {
fn config(&mut self, config: &mut rustc_interface::interface::Config) {
config.make_codegen_backend = Some(Box::new(make_miri_codegen_backend));
}

fn after_analysis<'tcx>(
&mut self,
_: &rustc_interface::interface::Compiler,
Expand Down Expand Up @@ -249,11 +279,20 @@ struct MiriBeRustCompilerCalls {
impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
#[allow(rustc::potential_query_instability)] // rustc_codegen_ssa (where this code is copied from) also allows this lint
fn config(&mut self, config: &mut Config) {
if config.opts.prints.is_empty() && self.target_crate {
if !self.target_crate {
// For a host crate, we fully behave like rustc.
return;
}
// For a target crate, we emit an rlib that Miri can later consume.
config.make_codegen_backend = Some(Box::new(make_miri_codegen_backend));

// Avoid warnings about unsupported crate types. However, only do that we we are *not* being
// queried by cargo about the supported crate types so that cargo still receives the
// warnings it expects.
if config.opts.prints.is_empty() {
#[allow(rustc::bad_opt_access)] // tcx does not exist yet
{
let any_crate_types = !config.opts.crate_types.is_empty();
// Avoid warnings about unsupported crate types.
config
.opts
.crate_types
Expand All @@ -264,66 +303,63 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
assert!(!config.opts.crate_types.is_empty());
}
}

// Queries overridden here affect the data stored in `rmeta` files of dependencies,
// which will be used later in non-`MIRI_BE_RUSTC` mode.
config.override_queries = Some(|_, local_providers| {
// We need to add #[used] symbols to exported_symbols for `lookup_link_section`.
// FIXME handle this somehow in rustc itself to avoid this hack.
local_providers.exported_non_generic_symbols = |tcx, LocalCrate| {
let reachable_set = tcx.with_stable_hashing_context(|hcx| {
tcx.reachable_set(()).to_sorted(&hcx, true)
});
tcx.arena.alloc_from_iter(
// This is based on:
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
reachable_set.into_iter().filter_map(|&local_def_id| {
// Do the same filtering that rustc does:
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
// Otherwise it may cause unexpected behaviours and ICEs
// (https://github.com/rust-lang/rust/issues/86261).
let is_reachable_non_generic = matches!(
tcx.hir_node_by_def_id(local_def_id),
Node::Item(&hir::Item {
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn{ .. },
..
}) | Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(..),
..
})
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
);
if !is_reachable_non_generic {
return None;
}
let codegen_fn_attrs = tcx.codegen_fn_attrs(local_def_id);
if codegen_fn_attrs.contains_extern_indicator()
|| codegen_fn_attrs
.flags
.contains(CodegenFnAttrFlags::USED_COMPILER)
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
{
Some((
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
// Some dummy `SymbolExportInfo` here. We only use
// `exported_symbols` in shims/foreign_items.rs and the export info
// is ignored.
SymbolExportInfo {
level: SymbolExportLevel::C,
kind: SymbolExportKind::Text,
used: false,
rustc_std_internal_symbol: false,
},
))
} else {
None
}
}),
)
}
});
}

// Queries overridden here affect the data stored in `rmeta` files of dependencies,
// which will be used later in non-`MIRI_BE_RUSTC` mode.
config.override_queries = Some(|_, local_providers| {
// We need to add #[used] symbols to exported_symbols for `lookup_link_section`.
// FIXME handle this somehow in rustc itself to avoid this hack.
local_providers.exported_non_generic_symbols = |tcx, LocalCrate| {
let reachable_set = tcx
.with_stable_hashing_context(|hcx| tcx.reachable_set(()).to_sorted(&hcx, true));
tcx.arena.alloc_from_iter(
// This is based on:
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
reachable_set.into_iter().filter_map(|&local_def_id| {
// Do the same filtering that rustc does:
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
// Otherwise it may cause unexpected behaviours and ICEs
// (https://github.com/rust-lang/rust/issues/86261).
let is_reachable_non_generic = matches!(
tcx.hir_node_by_def_id(local_def_id),
Node::Item(&hir::Item {
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn{ .. },
..
}) | Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(..),
..
})
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
);
if !is_reachable_non_generic {
return None;
}
let codegen_fn_attrs = tcx.codegen_fn_attrs(local_def_id);
if codegen_fn_attrs.contains_extern_indicator()
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
{
Some((
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
// Some dummy `SymbolExportInfo` here. We only use
// `exported_symbols` in shims/foreign_items.rs and the export info
// is ignored.
SymbolExportInfo {
level: SymbolExportLevel::C,
kind: SymbolExportKind::Text,
used: false,
rustc_std_internal_symbol: false,
},
))
} else {
None
}
}),
)
}
});
}

fn after_analysis<'tcx>(
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ pub use crate::shims::unwind::{CatchUnwindData, EvalContextExt as _};
/// Also disable the MIR pass that inserts an alignment check on every pointer dereference. Miri
/// does that too, and with a better error message.
pub const MIRI_DEFAULT_ARGS: &[&str] = &[
"-Zcodegen-backend=dummy",
"--cfg=miri",
"-Zalways-encode-mir",
"-Zextra-const-ub-checks",
Expand Down
2 changes: 0 additions & 2 deletions tests/fail/intrinsics/unchecked_shl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(unchecked_shifts)]

fn main() {
unsafe {
let _n = 1i8.unchecked_shl(8);
Expand Down
2 changes: 0 additions & 2 deletions tests/fail/intrinsics/unchecked_shr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(unchecked_shifts)]

fn main() {
unsafe {
let _n = 1i64.unchecked_shr(64);
Expand Down
16 changes: 16 additions & 0 deletions tests/pass/target-cpu-implies-target-feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test that target-cpu implies the correct target features
//@only-target: x86_64
//@compile-flags: -C target-cpu=x86-64-v4

fn main() {
assert!(cfg!(target_feature = "avx2"));
assert!(cfg!(target_feature = "avx512bw"));
assert!(cfg!(target_feature = "avx512cd"));
assert!(cfg!(target_feature = "avx512dq"));
assert!(cfg!(target_feature = "avx512f"));
assert!(cfg!(target_feature = "avx512vl"));
assert!(is_x86_feature_detected!("avx512bw"));

assert!(cfg!(not(target_feature = "avx512vpopcntdq")));
assert!(!is_x86_feature_detected!("avx512vpopcntdq"));
}
8 changes: 8 additions & 0 deletions tests/pass/target-spec-implies-target-feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Ensure that the target features given in the target spec are actually enabled.
//@only-target: armv7

fn main() {
assert!(cfg!(target_feature = "v7"));
assert!(cfg!(target_feature = "vfp2"));
assert!(cfg!(target_feature = "thumb2"));
}