Skip to content

Commit 92a1b0c

Browse files
committed
Auto merge of #157375 - JonathanBrouwer:rollup-2eWHhZL, r=<try>
Rollup of 15 pull requests try-job: dist-various-1 try-job: test-various try-job: x86_64-gnu-aux try-job: x86_64-gnu-llvm-21-3 try-job: x86_64-msvc-1 try-job: aarch64-apple try-job: x86_64-mingw-1 try-job: i686-msvc-2
2 parents 53509ca + a72cf41 commit 92a1b0c

163 files changed

Lines changed: 2632 additions & 2023 deletions

File tree

Some content is hidden

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

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
408408
let block_id = self.lower_body(|this| {
409409
let mut parameters: Vec<hir::Param<'_>> = Vec::with_capacity(param_count);
410410
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
411+
let mut stmts: &[hir::Stmt<'hir>] = &[];
411412

412413
for idx in 0..param_count {
413414
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
414415
parameters.push(param);
415416

417+
let generate_arg =
418+
|this: &mut Self| this.generate_arg(is_method, idx, param.pat.hir_id, span);
419+
416420
let arg = if let Some(block) = block
417421
&& idx == 0
418422
{
@@ -424,10 +428,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
424428
self_resolver.visit_block(block);
425429
// Target expr needs to lower `self` path.
426430
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
427-
this.lower_target_expr(&block)
431+
432+
// Lower with `HirId::INVALID` as we will use only expr and stmts.
433+
// FIXME(fn_delegation): Alternatives for target expression lowering:
434+
// https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2197170600.
435+
let block = this.lower_block_noalloc(HirId::INVALID, block, false);
436+
437+
stmts = block.stmts;
438+
439+
// The behavior of the delegation's target expression differs from the
440+
// behavior of the usual block, where if there is no final expression
441+
// the `()` is returned. In case of the similar situation in delegation
442+
// (no final expression) we propagate first argument instead of replacing
443+
// it with `()`.
444+
if let Some(&expr) = block.expr { expr } else { generate_arg(this) }
428445
} else {
429-
this.generate_arg(is_method, idx, param.pat.hir_id, span)
446+
generate_arg(this)
430447
};
448+
431449
args.push(arg);
432450
}
433451

@@ -439,11 +457,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
439457
if param_count == 0
440458
&& let Some(block) = block
441459
{
442-
args.push(this.lower_target_expr(&block));
460+
args.push(this.lower_block_expr(&block));
443461
}
444462

445463
let (final_expr, hir_id) =
446-
this.finalize_body_lowering(delegation, args, generics, span);
464+
this.finalize_body_lowering(delegation, stmts, args, generics, span);
447465

448466
call_expr_id = hir_id;
449467

@@ -455,22 +473,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
455473
(block_id, call_expr_id)
456474
}
457475

458-
// FIXME(fn_delegation): Alternatives for target expression lowering:
459-
// https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2197170600.
460-
fn lower_target_expr(&mut self, block: &Block) -> hir::Expr<'hir> {
461-
if let [stmt] = block.stmts.as_slice()
462-
&& let StmtKind::Expr(expr) = &stmt.kind
463-
{
464-
return self.lower_expr_mut(expr);
465-
}
466-
467-
let block = self.lower_block(block, false);
468-
self.mk_expr(hir::ExprKind::Block(block, None), block.span)
469-
}
470-
471476
fn finalize_body_lowering(
472477
&mut self,
473478
delegation: &Delegation,
479+
stmts: &'hir [hir::Stmt<'hir>],
474480
args: Vec<hir::Expr<'hir>>,
475481
generics: &mut GenericsGenerationResults<'hir>,
476482
span: Span,
@@ -522,7 +528,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
522528
let call = self.arena.alloc(self.mk_expr(hir::ExprKind::Call(callee_path, args), span));
523529

524530
let block = self.arena.alloc(hir::Block {
525-
stmts: &[],
531+
stmts,
526532
expr: Some(call),
527533
hir_id: self.next_id(),
528534
rules: hir::BlockCheckMode::DefaultBlock,
@@ -587,7 +593,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
587593

588594
let callee_path = this.arena.alloc(this.mk_expr(hir::ExprKind::Path(path), span));
589595
let args = if let Some(block) = delegation.body.as_ref() {
590-
this.arena.alloc_slice(&[this.lower_target_expr(block)])
596+
this.arena.alloc_slice(&[this.lower_block_expr(block)])
591597
} else {
592598
&mut []
593599
};

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::attributes::AttributeSafety;
88
use crate::session_diagnostics::{
99
EmptyExportName, NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass,
1010
NullOnObjcSelector, ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
11+
SanitizeInvalidStatic,
1112
};
1213
use crate::target_checking::Policy::AllowSilent;
1314

@@ -566,8 +567,18 @@ pub(crate) struct SanitizeParser;
566567

567568
impl SingleAttributeParser for SanitizeParser {
568569
const PATH: &[Symbol] = &[sym::sanitize];
569-
// FIXME: still checked in check_attrs.rs
570-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
570+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
571+
Allow(Target::Fn),
572+
Allow(Target::Closure),
573+
Allow(Target::Method(MethodKind::Inherent)),
574+
Allow(Target::Method(MethodKind::Trait { body: true })),
575+
Allow(Target::Method(MethodKind::TraitImpl)),
576+
Allow(Target::Impl { of_trait: false }),
577+
Allow(Target::Impl { of_trait: true }),
578+
Allow(Target::Mod),
579+
Allow(Target::Crate),
580+
Allow(Target::Static),
581+
]);
571582
const TEMPLATE: AttributeTemplate = template!(List: &[
572583
r#"address = "on|off""#,
573584
r#"kernel_address = "on|off""#,
@@ -668,6 +679,18 @@ impl SingleAttributeParser for SanitizeParser {
668679
}
669680
}
670681

682+
// The sanitizer attribute is only allowed on statics, if only address bits are set
683+
let all_set_except_address =
684+
(on_set | off_set) & !(SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS);
685+
if cx.target == Target::Static
686+
&& let Some(set) = all_set_except_address.iter().next()
687+
{
688+
cx.emit_err(SanitizeInvalidStatic {
689+
span: cx.attr_span,
690+
field: set.as_str().expect("Since this `SanitizerSet` is returned from an iterator, exactly one field is set")
691+
});
692+
}
693+
671694
Some(AttributeKind::Sanitize { on_set, off_set, rtsan, span: cx.attr_span })
672695
}
673696
}

compiler/rustc_attr_parsing/src/attributes/dummy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ use rustc_span::{Symbol, sym};
55
use crate::attributes::{OnDuplicate, SingleAttributeParser};
66
use crate::context::AcceptContext;
77
use crate::parser::ArgParser;
8-
use crate::target_checking::{ALL_TARGETS, AllowedTargets};
8+
use crate::target_checking::AllowedTargets;
99
use crate::unstable;
1010

1111
pub(crate) struct RustcDummyParser;
1212
impl SingleAttributeParser for RustcDummyParser {
1313
const PATH: &[Symbol] = &[sym::rustc_dummy];
1414
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Ignore;
15-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
15+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::ManuallyChecked;
1616
const TEMPLATE: AttributeTemplate = template!(Word); // Anything, really
1717
const STABILITY: AttributeStability =
1818
unstable!(rustc_attrs, "the `#[rustc_dummy]` attribute is used for rustc unit tests");
1919

20-
fn convert(_: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
20+
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
2121
args.ignore_args();
22+
cx.ignore_target_checks();
2223
Some(AttributeKind::RustcDummy)
2324
}
2425
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,12 @@ pub(crate) enum InvalidMachoSectionReason {
10231023
#[note("section name `{$section}` is longer than 16 bytes")]
10241024
SectionTooLong { section: String },
10251025
}
1026+
1027+
#[derive(Diagnostic)]
1028+
#[diag("`#[sanitize({$field} = ...)]` attribute cannot be used on statics")]
1029+
#[help("`#[sanitize]` can be used on statics if only the address is sanitized")]
1030+
pub(crate) struct SanitizeInvalidStatic {
1031+
#[primary_span]
1032+
pub span: Span,
1033+
pub field: &'static str,
1034+
}

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ pub(crate) fn allowed_targets_applied(
361361
Target::Method(MethodKind::Trait { body: true }),
362362
Target::Method(MethodKind::TraitImpl),
363363
];
364+
const FUNCTION_WITH_BODY_LIKE: &[Target] = &[
365+
Target::Fn,
366+
Target::Closure,
367+
Target::Method(MethodKind::Inherent),
368+
Target::Method(MethodKind::Trait { body: true }),
369+
Target::Method(MethodKind::TraitImpl),
370+
];
364371
const METHOD_LIKE: &[Target] = &[
365372
Target::Method(MethodKind::Inherent),
366373
Target::Method(MethodKind::Trait { body: false }),
@@ -379,6 +386,13 @@ pub(crate) fn allowed_targets_applied(
379386
target,
380387
&mut added_fake_targets,
381388
);
389+
filter_targets(
390+
&mut allowed_targets,
391+
FUNCTION_WITH_BODY_LIKE,
392+
"functions with a body",
393+
target,
394+
&mut added_fake_targets,
395+
);
382396
filter_targets(&mut allowed_targets, METHOD_LIKE, "methods", target, &mut added_fake_targets);
383397
filter_targets(&mut allowed_targets, IMPL_LIKE, "impl blocks", target, &mut added_fake_targets);
384398
filter_targets(&mut allowed_targets, ADT_LIKE, "data types", target, &mut added_fake_targets);
@@ -422,11 +436,15 @@ impl<'f, 'sess> AcceptContext<'f, 'sess> {
422436
attribute_args: &'static str,
423437
allowed_targets: &AllowedTargets,
424438
) {
439+
self.ignore_target_checks();
440+
AttributeParser::check_target(allowed_targets, attribute_args, self);
441+
}
442+
443+
pub(crate) fn ignore_target_checks(&mut self) {
425444
#[cfg(debug_assertions)]
426445
{
427446
self.has_target_been_checked = true;
428447
}
429-
AttributeParser::check_target(allowed_targets, attribute_args, self);
430448
}
431449
}
432450

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_session::config::{
2626
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet,
2727
};
2828
use rustc_span::{DUMMY_SP, Span, Spanned, Symbol, sym};
29-
use rustc_symbol_mangling::mangle_internal_symbol;
3029
use rustc_target::spec::{
3130
Arch, CfgAbi, Env, FramePointer, HasTargetSpec, Os, RelocModel, SmallDataThresholdSupport,
3231
Target, TlsModel,
@@ -136,7 +135,6 @@ pub(crate) struct FullCx<'ll, 'tcx> {
136135
pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
137136

138137
eh_personality: Cell<Option<&'ll Value>>,
139-
eh_catch_typeinfo: Cell<Option<&'ll Value>>,
140138
pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
141139

142140
intrinsics:
@@ -672,7 +670,6 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
672670
coverage_cx,
673671
dbg_cx,
674672
eh_personality: Cell::new(None),
675-
eh_catch_typeinfo: Cell::new(None),
676673
rust_try_fn: Cell::new(None),
677674
intrinsics: Default::default(),
678675
local_gen_sym_counter: Cell::new(0),
@@ -1042,23 +1039,6 @@ impl<'ll> CodegenCx<'ll, '_> {
10421039
}
10431040
}
10441041
}
1045-
1046-
pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
1047-
if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
1048-
return eh_catch_typeinfo;
1049-
}
1050-
let tcx = self.tcx;
1051-
assert!(self.sess().target.os == Os::Emscripten);
1052-
let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
1053-
Some(def_id) => self.get_static(def_id),
1054-
_ => {
1055-
let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
1056-
self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
1057-
}
1058-
};
1059-
self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
1060-
eh_catch_typeinfo
1061-
}
10621042
}
10631043

10641044
impl CodegenCx<'_, '_> {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
328328
.map(|field_index| {
329329
let coroutine_saved_local = coroutine_layout.variant_fields[variant_index]
330330
[FieldIdx::from_usize(field_index)];
331-
let field_name_maybe = coroutine_layout.field_names[coroutine_saved_local];
331+
let field_name_maybe =
332+
coroutine_layout.field_tys[coroutine_saved_local].debuginfo_name;
332333
let field_name = field_name_maybe
333334
.as_ref()
334335
.map(|s| Cow::from(s.as_str()))

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_session::lint::builtin::DEPRECATED_LLVM_INTRINSIC;
2828
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
2929
use rustc_symbol_mangling::{mangle_internal_symbol, symbol_name_for_instance_in_crate};
3030
use rustc_target::callconv::PassMode;
31-
use rustc_target::spec::{Arch, Os};
31+
use rustc_target::spec::Arch;
3232
use tracing::debug;
3333

3434
use crate::abi::FnAbiLlvmExt;
@@ -1356,8 +1356,6 @@ fn catch_unwind_intrinsic<'ll, 'tcx>(
13561356
codegen_msvc_try(bx, try_func, data, catch_func)
13571357
} else if wants_wasm_eh(bx.sess()) {
13581358
codegen_wasm_try(bx, try_func, data, catch_func)
1359-
} else if bx.sess().target.os == Os::Emscripten {
1360-
codegen_emcc_try(bx, try_func, data, catch_func)
13611359
} else {
13621360
codegen_gnu_try(bx, try_func, data, catch_func)
13631361
}
@@ -1654,87 +1652,6 @@ fn codegen_gnu_try<'ll, 'tcx>(
16541652
ret
16551653
}
16561654

1657-
// Variant of codegen_gnu_try used for emscripten where Rust panics are
1658-
// implemented using C++ exceptions. Here we use exceptions of a specific type
1659-
// (`struct rust_panic`) to represent Rust panics.
1660-
fn codegen_emcc_try<'ll, 'tcx>(
1661-
bx: &mut Builder<'_, 'll, 'tcx>,
1662-
try_func: &'ll Value,
1663-
data: &'ll Value,
1664-
catch_func: &'ll Value,
1665-
) -> &'ll Value {
1666-
let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
1667-
// Codegens the shims described above:
1668-
//
1669-
// bx:
1670-
// invoke %try_func(%data) normal %normal unwind %catch
1671-
//
1672-
// normal:
1673-
// ret 0
1674-
//
1675-
// catch:
1676-
// (%ptr, %selector) = landingpad
1677-
// %rust_typeid = @llvm.eh.typeid.for(@_ZTI10rust_panic)
1678-
// %is_rust_panic = %selector == %rust_typeid
1679-
// %catch_data = alloca { i8*, i8 }
1680-
// %catch_data[0] = %ptr
1681-
// %catch_data[1] = %is_rust_panic
1682-
// call %catch_func(%data, %catch_data)
1683-
// ret 1
1684-
let then = bx.append_sibling_block("then");
1685-
let catch = bx.append_sibling_block("catch");
1686-
1687-
let try_func = llvm::get_param(bx.llfn(), 0);
1688-
let data = llvm::get_param(bx.llfn(), 1);
1689-
let catch_func = llvm::get_param(bx.llfn(), 2);
1690-
let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1691-
bx.invoke(try_func_ty, None, None, try_func, &[data], then, catch, None, None);
1692-
1693-
bx.switch_to_block(then);
1694-
bx.ret(bx.const_bool(false));
1695-
1696-
// Type indicator for the exception being thrown.
1697-
//
1698-
// The first value in this tuple is a pointer to the exception object
1699-
// being thrown. The second value is a "selector" indicating which of
1700-
// the landing pad clauses the exception's type had been matched to.
1701-
bx.switch_to_block(catch);
1702-
let tydesc = bx.eh_catch_typeinfo();
1703-
let lpad_ty = bx.type_struct(&[bx.type_ptr(), bx.type_i32()], false);
1704-
let vals = bx.landing_pad(lpad_ty, bx.eh_personality(), 2);
1705-
bx.add_clause(vals, tydesc);
1706-
bx.add_clause(vals, bx.const_null(bx.type_ptr()));
1707-
let ptr = bx.extract_value(vals, 0);
1708-
let selector = bx.extract_value(vals, 1);
1709-
1710-
// Check if the typeid we got is the one for a Rust panic.
1711-
let rust_typeid = bx.call_intrinsic("llvm.eh.typeid.for", &[bx.val_ty(tydesc)], &[tydesc]);
1712-
let is_rust_panic = bx.icmp(IntPredicate::IntEQ, selector, rust_typeid);
1713-
let is_rust_panic = bx.zext(is_rust_panic, bx.type_bool());
1714-
1715-
// We need to pass two values to catch_func (ptr and is_rust_panic), so
1716-
// create an alloca and pass a pointer to that.
1717-
let ptr_size = bx.tcx().data_layout.pointer_size();
1718-
let ptr_align = bx.tcx().data_layout.pointer_align().abi;
1719-
let i8_align = bx.tcx().data_layout.i8_align;
1720-
// Required in order for there to be no padding between the fields.
1721-
assert!(i8_align <= ptr_align);
1722-
let catch_data = bx.alloca(2 * ptr_size, ptr_align);
1723-
bx.store(ptr, catch_data, ptr_align);
1724-
let catch_data_1 = bx.inbounds_ptradd(catch_data, bx.const_usize(ptr_size.bytes()));
1725-
bx.store(is_rust_panic, catch_data_1, i8_align);
1726-
1727-
let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
1728-
bx.call(catch_ty, None, None, catch_func, &[data, catch_data], None, None);
1729-
bx.ret(bx.const_bool(true));
1730-
});
1731-
1732-
// Note that no invoke is used here because by definition this function
1733-
// can't panic (that's what it's catching).
1734-
let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
1735-
ret
1736-
}
1737-
17381655
// Helper function to give a Block to a closure to codegen a shim function.
17391656
// This is currently primarily used for the `try` intrinsic functions above.
17401657
fn gen_fn<'a, 'll, 'tcx>(

0 commit comments

Comments
 (0)