Skip to content

Commit 100fde5

Browse files
committed
Auto merge of rust-lang#129691 - matthiaskrgr:rollup-owlcr3m, r=matthiaskrgr
Rollup of 11 pull requests Successful merges: - rust-lang#129421 (add repr to the allowlist for naked functions) - rust-lang#129480 (docs: correct panic conditions for rem_euclid and similar functions) - rust-lang#129551 (ub_checks intrinsics: fall back to cfg(ub_checks)) - rust-lang#129608 (const-eval: do not make UbChecks behavior depend on current crate's flags) - rust-lang#129613 (interpret: do not make const-eval query result depend on tcx.sess) - rust-lang#129641 (rustdoc: fix missing resource suffix on `crates.js`) - rust-lang#129657 (Rename `BikeshedIntrinsicFrom` to `TransmuteFrom`) - rust-lang#129666 (interpret: add missing alignment check in raw_eq) - rust-lang#129667 (Rustc driver cleanup) - rust-lang#129668 (Fix Pin::set bounds regression) - rust-lang#129686 (coverage: Rename `CodeRegion` to `SourceRegion`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ac77e88 + 4854fa7 commit 100fde5

File tree

145 files changed

+899
-701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+899
-701
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::mir::coverage::{
2-
CodeRegion, ConditionInfo, CounterId, CovTerm, DecisionInfo, ExpressionId, MappingKind,
2+
ConditionInfo, CounterId, CovTerm, DecisionInfo, ExpressionId, MappingKind, SourceRegion,
33
};
44

55
/// Must match the layout of `LLVMRustCounterKind`.
@@ -236,9 +236,10 @@ impl CounterMappingRegion {
236236
pub(crate) fn from_mapping(
237237
mapping_kind: &MappingKind,
238238
local_file_id: u32,
239-
code_region: &CodeRegion,
239+
source_region: &SourceRegion,
240240
) -> Self {
241-
let &CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = code_region;
241+
let &SourceRegion { file_name: _, start_line, start_col, end_line, end_col } =
242+
source_region;
242243
match *mapping_kind {
243244
MappingKind::Code(term) => Self::code_region(
244245
Counter::from_term(term),

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_data_structures::captures::Captures;
22
use rustc_data_structures::fx::FxIndexSet;
33
use rustc_index::bit_set::BitSet;
44
use rustc_middle::mir::coverage::{
5-
CodeRegion, CounterId, CovTerm, Expression, ExpressionId, FunctionCoverageInfo, Mapping,
6-
MappingKind, Op,
5+
CounterId, CovTerm, Expression, ExpressionId, FunctionCoverageInfo, Mapping, MappingKind, Op,
6+
SourceRegion,
77
};
88
use rustc_middle::ty::Instance;
99
use rustc_span::Symbol;
@@ -201,7 +201,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
201201

202202
/// Returns an iterator over all filenames used by this function's mappings.
203203
pub(crate) fn all_file_names(&self) -> impl Iterator<Item = Symbol> + Captures<'_> {
204-
self.function_coverage_info.mappings.iter().map(|mapping| mapping.code_region.file_name)
204+
self.function_coverage_info.mappings.iter().map(|mapping| mapping.source_region.file_name)
205205
}
206206

207207
/// Convert this function's coverage expression data into a form that can be
@@ -230,12 +230,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
230230
/// that will be used by `mapgen` when preparing for FFI.
231231
pub(crate) fn counter_regions(
232232
&self,
233-
) -> impl Iterator<Item = (MappingKind, &CodeRegion)> + ExactSizeIterator {
233+
) -> impl Iterator<Item = (MappingKind, &SourceRegion)> + ExactSizeIterator {
234234
self.function_coverage_info.mappings.iter().map(move |mapping| {
235-
let Mapping { kind, code_region } = mapping;
235+
let Mapping { kind, source_region } = mapping;
236236
let kind =
237237
kind.map_terms(|term| if self.is_zero_term(term) { CovTerm::Zero } else { term });
238-
(kind, code_region)
238+
(kind, source_region)
239239
})
240240
}
241241

compiler/rustc_const_eval/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,6 @@ const_eval_unallowed_mutable_refs =
402402
const_eval_unallowed_op_in_const_context =
403403
{$msg}
404404
405-
const_eval_unavailable_target_features_for_fn =
406-
calling a function that requires unavailable target features: {$unavailable_feats}
407-
408405
const_eval_uninhabited_enum_variant_read =
409406
read discriminant of an uninhabited enum variant
410407
const_eval_uninhabited_enum_variant_written =

compiler/rustc_const_eval/src/interpret/call.rs

+7-37
Original file line numberDiff line numberDiff line change
@@ -311,34 +311,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
311311
Ok(())
312312
}
313313

314-
fn check_fn_target_features(&self, instance: ty::Instance<'tcx>) -> InterpResult<'tcx, ()> {
315-
// Calling functions with `#[target_feature]` is not unsafe on WASM, see #84988
316-
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
317-
if !self.tcx.sess.target.is_like_wasm
318-
&& attrs
319-
.target_features
320-
.iter()
321-
.any(|feature| !self.tcx.sess.target_features.contains(&feature.name))
322-
{
323-
throw_ub_custom!(
324-
fluent::const_eval_unavailable_target_features_for_fn,
325-
unavailable_feats = attrs
326-
.target_features
327-
.iter()
328-
.filter(|&feature| !feature.implied
329-
&& !self.tcx.sess.target_features.contains(&feature.name))
330-
.fold(String::new(), |mut s, feature| {
331-
if !s.is_empty() {
332-
s.push_str(", ");
333-
}
334-
s.push_str(feature.name.as_str());
335-
s
336-
}),
337-
);
338-
}
339-
Ok(())
340-
}
341-
342314
/// The main entry point for creating a new stack frame: performs ABI checks and initializes
343315
/// arguments.
344316
#[instrument(skip(self), level = "trace")]
@@ -360,20 +332,18 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
360332
throw_unsup_format!("calling a c-variadic function is not supported");
361333
}
362334

363-
if M::enforce_abi(self) {
364-
if caller_fn_abi.conv != callee_fn_abi.conv {
365-
throw_ub_custom!(
366-
fluent::const_eval_incompatible_calling_conventions,
367-
callee_conv = format!("{:?}", callee_fn_abi.conv),
368-
caller_conv = format!("{:?}", caller_fn_abi.conv),
369-
)
370-
}
335+
if caller_fn_abi.conv != callee_fn_abi.conv {
336+
throw_ub_custom!(
337+
fluent::const_eval_incompatible_calling_conventions,
338+
callee_conv = format!("{:?}", callee_fn_abi.conv),
339+
caller_conv = format!("{:?}", caller_fn_abi.conv),
340+
)
371341
}
372342

373343
// Check that all target features required by the callee (i.e., from
374344
// the attribute `#[target_feature(enable = ...)]`) are enabled at
375345
// compile time.
376-
self.check_fn_target_features(instance)?;
346+
M::check_fn_target_features(self, instance)?;
377347

378348
if !callee_fn_abi.can_unwind {
379349
// The callee cannot unwind, so force the `Unreachable` unwind handling.

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -684,19 +684,19 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
684684
assert!(layout.is_sized());
685685

686686
let get_bytes = |this: &InterpCx<'tcx, M>,
687-
op: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
688-
size|
687+
op: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>|
689688
-> InterpResult<'tcx, &[u8]> {
690689
let ptr = this.read_pointer(op)?;
691-
let Some(alloc_ref) = self.get_ptr_alloc(ptr, size)? else {
690+
this.check_ptr_align(ptr, layout.align.abi)?;
691+
let Some(alloc_ref) = self.get_ptr_alloc(ptr, layout.size)? else {
692692
// zero-sized access
693693
return Ok(&[]);
694694
};
695695
alloc_ref.get_bytes_strip_provenance()
696696
};
697697

698-
let lhs_bytes = get_bytes(self, lhs, layout.size)?;
699-
let rhs_bytes = get_bytes(self, rhs, layout.size)?;
698+
let lhs_bytes = get_bytes(self, lhs)?;
699+
let rhs_bytes = get_bytes(self, rhs)?;
700700
Ok(Scalar::from_bool(lhs_bytes == rhs_bytes))
701701
}
702702
}

compiler/rustc_const_eval/src/interpret/machine.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ pub trait Machine<'tcx>: Sized {
173173
false
174174
}
175175

176-
/// Whether function calls should be [ABI](CallAbi)-checked.
177-
fn enforce_abi(_ecx: &InterpCx<'tcx, Self>) -> bool {
178-
true
179-
}
180-
181176
/// Whether Assert(OverflowNeg) and Assert(Overflow) MIR terminators should actually
182177
/// check for overflow.
183178
fn ignore_optional_overflow_checks(_ecx: &InterpCx<'tcx, Self>) -> bool;
@@ -238,6 +233,13 @@ pub trait Machine<'tcx>: Sized {
238233
unwind: mir::UnwindAction,
239234
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>>;
240235

236+
/// Check whether the given function may be executed on the current machine, in terms of the
237+
/// target features is requires.
238+
fn check_fn_target_features(
239+
_ecx: &InterpCx<'tcx, Self>,
240+
_instance: ty::Instance<'tcx>,
241+
) -> InterpResult<'tcx>;
242+
241243
/// Called to evaluate `Assert` MIR terminators that trigger a panic.
242244
fn assert_panic(
243245
ecx: &mut InterpCx<'tcx, Self>,
@@ -280,6 +282,9 @@ pub trait Machine<'tcx>: Sized {
280282
Ok(())
281283
}
282284

285+
/// Determines the result of a `NullaryOp::UbChecks` invocation.
286+
fn ub_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
287+
283288
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
284289
/// You can use this to detect long or endlessly running programs.
285290
#[inline]
@@ -614,6 +619,16 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
614619
unreachable!("unwinding cannot happen during compile-time evaluation")
615620
}
616621

622+
#[inline(always)]
623+
fn check_fn_target_features(
624+
_ecx: &InterpCx<$tcx, Self>,
625+
_instance: ty::Instance<$tcx>,
626+
) -> InterpResult<$tcx> {
627+
// For now we don't do any checking here. We can't use `tcx.sess` because that can differ
628+
// between crates, and we need to ensure that const-eval always behaves the same.
629+
Ok(())
630+
}
631+
617632
#[inline(always)]
618633
fn call_extra_fn(
619634
_ecx: &mut InterpCx<$tcx, Self>,
@@ -627,6 +642,13 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
627642
match fn_val {}
628643
}
629644

645+
#[inline(always)]
646+
fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
647+
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
648+
// unsound differences in evaluating the same constant at different instantiation sites.
649+
Ok(true)
650+
}
651+
630652
#[inline(always)]
631653
fn adjust_global_allocation<'b>(
632654
_ecx: &InterpCx<$tcx, Self>,

compiler/rustc_const_eval/src/interpret/operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
512512
self.tcx.offset_of_subfield(self.param_env, layout, fields.iter()).bytes();
513513
ImmTy::from_uint(val, usize_layout())
514514
}
515-
UbChecks => ImmTy::from_bool(self.tcx.sess.ub_checks(), *self.tcx),
515+
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),
516516
})
517517
}
518518
}

compiler/rustc_driver_impl/src/lib.rs

+36-29
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,17 @@ fn run_compiler(
393393

394394
let linker = compiler.enter(|queries| {
395395
let early_exit = || early_exit().map(|_| None);
396+
397+
// Parse the crate root source code (doesn't parse submodules yet)
398+
// Everything else is parsed during macro expansion.
396399
queries.parse()?;
397400

398-
if let Some(ppm) = &sess.opts.pretty {
399-
if ppm.needs_ast_map() {
401+
// If pretty printing is requested: Figure out the representation, print it and exit
402+
if let Some(pp_mode) = sess.opts.pretty {
403+
if pp_mode.needs_ast_map() {
400404
queries.global_ctxt()?.enter(|tcx| {
401405
tcx.ensure().early_lint_checks(());
402-
pretty::print(sess, *ppm, pretty::PrintExtra::NeedsAstMap { tcx });
406+
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
403407
Ok(())
404408
})?;
405409

@@ -410,7 +414,7 @@ fn run_compiler(
410414
let krate = queries.parse()?;
411415
pretty::print(
412416
sess,
413-
*ppm,
417+
pp_mode,
414418
pretty::PrintExtra::AfterParsing { krate: &*krate.borrow() },
415419
);
416420
}
@@ -465,12 +469,8 @@ fn run_compiler(
465469
linker.link(sess, codegen_backend)?
466470
}
467471

468-
if sess.opts.unstable_opts.print_fuel.is_some() {
469-
eprintln!(
470-
"Fuel used by {}: {}",
471-
sess.opts.unstable_opts.print_fuel.as_ref().unwrap(),
472-
sess.print_fuel.load(Ordering::SeqCst)
473-
);
472+
if let Some(fuel) = sess.opts.unstable_opts.print_fuel.as_deref() {
473+
eprintln!("Fuel used by {}: {}", fuel, sess.print_fuel.load(Ordering::SeqCst));
474474
}
475475

476476
Ok(())
@@ -487,36 +487,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
487487
(odir, ofile)
488488
}
489489

490-
// Extract input (string or file and optional path) from matches.
490+
/// Extract input (string or file and optional path) from matches.
491+
/// This handles reading from stdin if `-` is provided.
491492
fn make_input(
492493
early_dcx: &EarlyDiagCtxt,
493494
free_matches: &[String],
494495
) -> Result<Option<Input>, ErrorGuaranteed> {
495-
let [ifile] = free_matches else { return Ok(None) };
496-
if ifile == "-" {
497-
let mut src = String::new();
498-
if io::stdin().read_to_string(&mut src).is_err() {
499-
// Immediately stop compilation if there was an issue reading
500-
// the input (for example if the input stream is not UTF-8).
501-
let reported =
502-
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
503-
return Err(reported);
504-
}
505-
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
496+
let [input_file] = free_matches else { return Ok(None) };
497+
498+
if input_file != "-" {
499+
// Normal `Input::File`
500+
return Ok(Some(Input::File(PathBuf::from(input_file))));
501+
}
502+
503+
// read from stdin as `Input::Str`
504+
let mut input = String::new();
505+
if io::stdin().read_to_string(&mut input).is_err() {
506+
// Immediately stop compilation if there was an issue reading
507+
// the input (for example if the input stream is not UTF-8).
508+
let reported =
509+
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
510+
return Err(reported);
511+
}
512+
513+
let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
514+
Ok(path) => {
506515
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
507516
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
508517
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
509518
);
510519
let line = isize::from_str_radix(&line, 10)
511520
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
512-
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
513-
Ok(Some(Input::Str { name: file_name, input: src }))
514-
} else {
515-
Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
521+
FileName::doc_test_source_code(PathBuf::from(path), line)
516522
}
517-
} else {
518-
Ok(Some(Input::File(PathBuf::from(ifile))))
519-
}
523+
Err(_) => FileName::anon_source_code(&input),
524+
};
525+
526+
Ok(Some(Input::Str { name, input }))
520527
}
521528

522529
/// Whether to stop or continue compilation.

compiler/rustc_middle/src/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ macro_rules! arena_types {
3535
)>,
3636
[] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,
3737
[] resolutions: rustc_middle::ty::ResolverGlobalCtxt,
38-
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
3938
[] const_allocs: rustc_middle::mir::interpret::Allocation,
4039
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,
4140
// Required for the incremental on-disk cache

compiler/rustc_middle/src/mir/coverage.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ impl Debug for CoverageKind {
163163

164164
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, Eq, PartialOrd, Ord)]
165165
#[derive(TypeFoldable, TypeVisitable)]
166-
pub struct CodeRegion {
166+
pub struct SourceRegion {
167167
pub file_name: Symbol,
168168
pub start_line: u32,
169169
pub start_col: u32,
170170
pub end_line: u32,
171171
pub end_col: u32,
172172
}
173173

174-
impl Debug for CodeRegion {
174+
impl Debug for SourceRegion {
175175
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
176176
write!(
177177
fmt,
@@ -242,7 +242,7 @@ impl MappingKind {
242242
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
243243
pub struct Mapping {
244244
pub kind: MappingKind,
245-
pub code_region: CodeRegion,
245+
pub source_region: SourceRegion,
246246
}
247247

248248
/// Stores per-function coverage information attached to a `mir::Body`,

compiler/rustc_middle/src/mir/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ fn write_function_coverage_info(
547547
for (id, expression) in expressions.iter_enumerated() {
548548
writeln!(w, "{INDENT}coverage {id:?} => {expression:?};")?;
549549
}
550-
for coverage::Mapping { kind, code_region } in mappings {
551-
writeln!(w, "{INDENT}coverage {kind:?} => {code_region:?};")?;
550+
for coverage::Mapping { kind, source_region } in mappings {
551+
writeln!(w, "{INDENT}coverage {kind:?} => {source_region:?};")?;
552552
}
553553
writeln!(w)?;
554554

compiler/rustc_middle/src/ty/codec.rs

-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ impl_decodable_via_ref! {
462462
&'tcx traits::ImplSource<'tcx, ()>,
463463
&'tcx mir::Body<'tcx>,
464464
&'tcx mir::BorrowCheckResult<'tcx>,
465-
&'tcx mir::coverage::CodeRegion,
466465
&'tcx ty::List<ty::BoundVariableKind>,
467466
&'tcx ty::ListWithCachedTypeInfo<ty::Clause<'tcx>>,
468467
&'tcx ty::List<FieldIdx>,

0 commit comments

Comments
 (0)