Skip to content

Commit 7618355

Browse files
committed
Auto merge of #163296 - jhpratt:rollup-30Dq02h, r=jhpratt
Rollup of 7 pull requests Successful merges: - #160068 (Preserve explicit register names in inline asm diagnostics) - #163239 (Fix applicability level for cfg suggestion) - #163025 (make `Complex` ABI GCC-compatible on sparc) - #163076 (powerpc64: don't pass unions in FPRs on ELFv1) - #163268 ([rustdoc] Correctly check that an item is not `doc(hidden)` with `--generate-link-to-definition`) - #163288 (Tweak Layout::from_size_align_unchecked safety comment) - #163292 (moving div_ceil() to associated items macro)
2 parents 0d38a84 + d7f0af0 commit 7618355

22 files changed

Lines changed: 583 additions & 119 deletions

File tree

‎compiler/rustc_abi/src/callconv.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
5858
///
5959
/// Note: We generally ignore 1-ZST fields when computing this value (see #56877).
6060
///
61+
/// Note: The fields of a union are merged as if it were a struct, so the result does not
62+
/// distinguish the two. `powerpc64::is_or_contains_union` in `rustc_target` walks the layout
63+
/// again to do that, mirroring the array handling here.
64+
///
6165
/// This is public so that it can be used in unit tests, but
6266
/// should generally only be relevant to the ABI details of
6367
/// specific targets.

‎compiler/rustc_ast_lowering/src/asm.rs‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
140140
.iter()
141141
.map(|(op, op_sp)| {
142142
let lower_reg = |&reg: &_| match reg {
143-
InlineAsmRegOrRegClass::Reg(reg) => {
144-
asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
143+
InlineAsmRegOrRegClass::Reg(reg) => hir::InlineAsmRegOrRegClass::Reg {
144+
reg: if let Some(asm_arch) = asm_arch {
145145
asm::InlineAsmReg::parse(asm_arch, reg).unwrap_or_else(|error| {
146146
self.dcx().emit_err(InvalidRegister {
147147
op_span: *op_sp,
@@ -152,10 +152,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
152152
})
153153
} else {
154154
asm::InlineAsmReg::Err
155-
})
156-
}
155+
},
156+
source_name: Some(reg),
157+
},
157158
InlineAsmRegOrRegClass::RegClass(reg_class) => {
158-
asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
159+
hir::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
159160
asm::InlineAsmRegClass::parse(asm_arch, reg_class).unwrap_or_else(
160161
|supported_register_classes| {
161162
self.dcx().emit_err(InvalidRegisterClass {
@@ -344,7 +345,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
344345
}
345346

346347
// Check for conflicts between explicit register operands.
347-
if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
348+
if let hir::InlineAsmRegOrRegClass::Reg { reg, .. } = reg {
348349
let (input, output) = match op {
349350
hir::InlineAsmOperand::In { .. } => (true, false),
350351

@@ -393,24 +394,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
393394
}
394395
_ => None,
395396
};
396-
let reg_str = |idx| -> &str {
397-
// HIR asm doesn't preserve the original alias string of the explicit register,
398-
// so we have to retrieve it from AST
399-
let (op, _): &(InlineAsmOperand, Span) = &asm.operands[idx];
400-
if let Some(ast::InlineAsmRegOrRegClass::Reg(reg_sym)) =
401-
op.reg()
402-
{
403-
reg_sym.as_str()
404-
} else {
405-
unreachable!("{op:?} is not a register operand");
406-
}
407-
};
397+
let reg1_name = op
398+
.reg()
399+
.and_then(|reg| reg.source_name())
400+
.expect("{op:?} has no source register name");
401+
402+
let reg2_name = op2
403+
.reg()
404+
.and_then(|reg| reg.source_name())
405+
.expect("{op2:?} has no source register name");
408406

409407
self.dcx().emit_err(RegisterConflict {
410408
op_span1: op_sp,
411409
op_span2: op_sp2,
412-
reg1_name: reg_str(idx),
413-
reg2_name: reg_str(idx2),
410+
reg1_name: reg1_name.as_str(),
411+
reg2_name: reg2_name.as_str(),
414412
in_out,
415413
});
416414
}
@@ -457,7 +455,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
457455
if !output_used {
458456
operands.push((
459457
hir::InlineAsmOperand::Out {
460-
reg: asm::InlineAsmRegOrRegClass::Reg(clobber),
458+
reg: hir::InlineAsmRegOrRegClass::Reg {
459+
reg: clobber,
460+
source_name: None,
461+
},
461462
late: true,
462463
expr: None,
463464
},

‎compiler/rustc_hir/src/hir.rs‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_span::{
3030
BytePos, DUMMY_SP, DesugaringKind, ErrorGuaranteed, Ident, LocalExpnId, Span, Spanned, Symbol,
3131
kw, sym,
3232
};
33-
use rustc_target::asm::InlineAsmRegOrRegClass;
33+
use rustc_target::asm;
3434
use tracing::debug;
3535

3636
use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res};
@@ -3611,6 +3611,44 @@ pub enum TyKind<'hir, Unambig = ()> {
36113611
Infer(Unambig),
36123612
}
36133613

3614+
/// Stores explicit register name from source
3615+
/// for diagnostics only.
3616+
#[derive(Debug, Clone, Copy, StableHash)]
3617+
pub enum InlineAsmRegOrRegClass {
3618+
Reg { reg: asm::InlineAsmReg, source_name: Option<Symbol> },
3619+
RegClass(asm::InlineAsmRegClass),
3620+
}
3621+
3622+
impl InlineAsmRegOrRegClass {
3623+
// For `rustc_mir_build` and `clippy_utils`
3624+
pub fn as_target(self) -> asm::InlineAsmRegOrRegClass {
3625+
match self {
3626+
Self::Reg { reg, .. } => asm::InlineAsmRegOrRegClass::Reg(reg),
3627+
Self::RegClass(reg_class) => asm::InlineAsmRegOrRegClass::RegClass(reg_class),
3628+
}
3629+
}
3630+
3631+
// For `rustc_ast_lowering`
3632+
pub fn reg_class(self) -> asm::InlineAsmRegClass {
3633+
self.as_target().reg_class()
3634+
}
3635+
3636+
// For `rustc_ast_lowering`
3637+
pub fn source_name(self) -> Option<Symbol> {
3638+
match self {
3639+
Self::Reg { source_name, .. } => source_name,
3640+
Self::RegClass(_) => None,
3641+
}
3642+
}
3643+
}
3644+
3645+
// For `rustc_hir_pretty`
3646+
impl fmt::Display for InlineAsmRegOrRegClass {
3647+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3648+
self.as_target().fmt(f)
3649+
}
3650+
}
3651+
36143652
#[derive(Debug, Clone, Copy, StableHash)]
36153653
pub enum InlineAsmOperand<'hir> {
36163654
In {
@@ -3665,7 +3703,7 @@ impl<'hir> InlineAsmOperand<'hir> {
36653703
pub fn is_clobber(&self) -> bool {
36663704
matches!(
36673705
self,
3668-
InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(_), late: _, expr: None }
3706+
InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg { .. }, expr: None, .. }
36693707
)
36703708
}
36713709
}

‎compiler/rustc_hir_typeck/src/inline_asm.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use rustc_middle::ty::{
1313
use rustc_span::def_id::LocalDefId;
1414
use rustc_span::{ErrorGuaranteed, Span, Symbol, bug, sym};
1515
use rustc_target::asm::{
16-
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmSize, InlineAsmType,
17-
ModifierInfo,
16+
InlineAsmReg, InlineAsmRegClass, InlineAsmSize, InlineAsmType, ModifierInfo,
1817
};
1918
use rustc_trait_selection::infer::InferCtxtExt;
2019

@@ -191,7 +190,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
191190
fn check_asm_operand_type(
192191
&self,
193192
idx: usize,
194-
reg: InlineAsmRegOrRegClass,
193+
reg: hir::InlineAsmRegOrRegClass,
195194
expr: &'tcx hir::Expr<'tcx>,
196195
template: &[InlineAsmTemplatePiece],
197196
is_input: bool,
@@ -477,7 +476,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
477476
if let Some(reg) = op.reg() {
478477
// Some explicit registers cannot be used depending on the
479478
// target. Reject those here.
480-
if let InlineAsmRegOrRegClass::Reg(reg) = reg {
479+
if let hir::InlineAsmRegOrRegClass::Reg { reg, source_name } = reg {
481480
if let InlineAsmReg::Err = reg {
482481
// `validate` will panic on `Err`, as an error must
483482
// already have been reported.
@@ -490,7 +489,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
490489
&self.tcx().sess.target,
491490
op.is_clobber(),
492491
) {
493-
let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
492+
let reg_name = match source_name {
493+
Some(ref reg_name) => reg_name.as_str(),
494+
None => &reg.name(),
495+
};
496+
let msg = format!("cannot use register `{reg_name}`: {msg}");
494497
self.fcx.dcx().span_err(op_sp, msg);
495498
continue;
496499
}

‎compiler/rustc_mir_build/src/thir/cx/expr.rs‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -827,22 +827,27 @@ impl<'tcx> ThirBuildCx<'tcx> {
827827
.operands
828828
.iter()
829829
.map(|(op, _op_sp)| match *op {
830-
hir::InlineAsmOperand::In { reg, expr } => {
831-
InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
832-
}
830+
hir::InlineAsmOperand::In { reg, expr } => InlineAsmOperand::In {
831+
reg: reg.as_target(),
832+
expr: self.mirror_expr(expr),
833+
},
833834
hir::InlineAsmOperand::Out { reg, late, ref expr } => {
834835
InlineAsmOperand::Out {
835-
reg,
836+
reg: reg.as_target(),
836837
late,
837838
expr: expr.map(|expr| self.mirror_expr(expr)),
838839
}
839840
}
840841
hir::InlineAsmOperand::InOut { reg, late, expr } => {
841-
InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
842+
InlineAsmOperand::InOut {
843+
reg: reg.as_target(),
844+
late,
845+
expr: self.mirror_expr(expr),
846+
}
842847
}
843848
hir::InlineAsmOperand::SplitInOut { reg, late, in_expr, ref out_expr } => {
844849
InlineAsmOperand::SplitInOut {
845-
reg,
850+
reg: reg.as_target(),
846851
late,
847852
in_expr: self.mirror_expr(in_expr),
848853
out_expr: out_expr.map(|expr| self.mirror_expr(expr)),

‎compiler/rustc_parse/src/diagnostics.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,8 @@ pub(crate) enum ExpectedSemiSugg {
16561656
style = "short"
16571657
)]
16581658
ChangeToSemi(#[primary_span] Span),
1659-
#[suggestion("add `;` here", code = ";", applicability = "machine-applicable", style = "short")]
1660-
AddSemi(#[primary_span] Span),
1659+
#[suggestion("add `;` here", code = ";", style = "short")]
1660+
AddSemi(#[primary_span] Span, #[applicability] Applicability),
16611661
}
16621662

16631663
#[derive(Diagnostic)]

‎compiler/rustc_parse/src/parser/diagnostics.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a> Parser<'a> {
439439
span,
440440
token: self.token,
441441
unexpected_token_label: Some(self.token.span),
442-
sugg: ExpectedSemiSugg::AddSemi(span),
442+
sugg: ExpectedSemiSugg::AddSemi(span, Applicability::MachineApplicable),
443443
});
444444
return Ok(guar);
445445
}
@@ -723,7 +723,8 @@ impl<'a> Parser<'a> {
723723
span,
724724
token: self.token,
725725
unexpected_token_label: Some(self.token.span),
726-
sugg: ExpectedSemiSugg::AddSemi(span),
726+
// A semicolon may discard the intended return value of a cfg-gated tail expression.
727+
sugg: ExpectedSemiSugg::AddSemi(span, Applicability::MaybeIncorrect),
727728
});
728729
let attr_span = match &expr.attrs[..] {
729730
[] => unreachable!(),
@@ -755,7 +756,7 @@ impl<'a> Parser<'a> {
755756
(expr.span.shrink_to_lo(), "{ ".to_string()),
756757
(expr.span.shrink_to_hi(), " }".to_string()),
757758
],
758-
Applicability::MachineApplicable,
759+
Applicability::MaybeIncorrect,
759760
);
760761

761762
// Special handling for `#[cfg(...)]` chains
@@ -811,7 +812,8 @@ impl<'a> Parser<'a> {
811812
"it seems like you are trying to provide different expressions depending on \
812813
`cfg`, consider using `if cfg!(..)`",
813814
sugg,
814-
Applicability::MachineApplicable,
815+
// Unlike `#[cfg]`, `if cfg!` still checks disabled branches.
816+
Applicability::MaybeIncorrect,
815817
);
816818
}
817819
}

‎compiler/rustc_target/src/callconv/powerpc64.rs‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Alignment of 128 bit types is not currently handled, this will
33
// need to be fixed when PowerPC vector support is added.
44

5-
use rustc_abi::{HasDataLayout, Integer, Numeric, TyAbiInterface};
5+
use rustc_abi::{FieldsShape, HasDataLayout, Integer, Numeric, TyAbiInterface, TyAndLayout};
66

77
use crate::callconv::{Align, ArgAbi, CastTarget, FnAbi, Reg, RegKind, Uniform};
88
use crate::spec::{HasTargetSpec, LlvmAbi, Os};
@@ -15,6 +15,41 @@ enum ABI {
1515
}
1616
use ABI::*;
1717

18+
/// Whether `layout` is or contains a union.
19+
///
20+
/// `homogeneous_aggregate` merges the fields of a union, so it cannot tell a union from a
21+
/// structure. This walks the layout a second time; keep the array handling here in sync with
22+
/// `homogeneous_aggregate`. ZSTs are ignored entirely, as both GCC and Clang do.
23+
///
24+
/// This does not look at enums that are represented as unions at the ABI level (e.g. a
25+
/// `#[repr(C)]` enum with fields). That does not matter here: enums can only have integer
26+
/// discriminants, and `is_homogeneous_aggregate` rejects anything containing an integer, so an
27+
/// enum can never be a float or vector homogeneous aggregate.
28+
fn is_or_contains_union<'a, Ty, C>(cx: &C, layout: TyAndLayout<'a, Ty>) -> bool
29+
where
30+
Ty: TyAbiInterface<'a, C> + Copy,
31+
C: HasDataLayout,
32+
{
33+
if layout.is_zst() {
34+
return false;
35+
}
36+
match layout.fields {
37+
FieldsShape::Primitive => false,
38+
// A `repr(transparent)` union is guaranteed to be ABI-compatible with its single
39+
// non-1-ZST field, so look through it instead of rejecting it.
40+
FieldsShape::Union(_) => {
41+
match layout.non_1zst_field(cx).filter(|_| layout.is_transparent()) {
42+
Some((_, field)) => is_or_contains_union(cx, field),
43+
None => true,
44+
}
45+
}
46+
FieldsShape::Array { .. } => is_or_contains_union(cx, layout.field(cx, 0)),
47+
FieldsShape::Arbitrary { .. } => {
48+
(0..layout.fields.count()).any(|i| is_or_contains_union(cx, layout.field(cx, i)))
49+
}
50+
}
51+
}
52+
1853
fn is_homogeneous_aggregate<'a, Ty, C>(
1954
cx: &C,
2055
arg: &mut ArgAbi<'a, Ty>,
@@ -27,7 +62,8 @@ where
2762
arg.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()).and_then(|unit| {
2863
// ELFv1 and AIX only passes one-member aggregates transparently.
2964
// ELFv2 passes up to eight uniquely addressable members.
30-
if ((abi == ELFv1 || abi == AIX) && arg.layout.size > unit.size)
65+
if ((abi == ELFv1 || abi == AIX)
66+
&& (arg.layout.size > unit.size || is_or_contains_union(cx, arg.layout)))
3167
|| arg.layout.size > unit.size.checked_mul(8, cx).unwrap()
3268
{
3369
return None;

0 commit comments

Comments
 (0)