Skip to content

Commit 8a9cccb

Browse files
committed
Auto merge of #127326 - matthiaskrgr:rollup-kz7vd3w, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #123043 (Disable dead variant removal for `#[repr(C)]` enums.) - #126405 (Migrate some rustc_builtin_macros to SessionDiagnostic) - #127037 (Remove some duplicated tests) - #127283 (Reject SmartPointer constructions not serving the purpose) - #127301 (Tweak some structured suggestions to be more verbose and accurate) - #127307 (Allow to have different types for arguments of `Rustc::remap_path_prefix`) - #127309 (jsondocck: add `$FILE` built-in variable) - #127314 (Trivial update on tidy bless note) - #127319 (Remove a use of `StructuredDiag`, which is incompatible with automatic error tainting and error translations) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 9f877c9 + dd42f7a commit 8a9cccb

File tree

117 files changed

+2642
-640
lines changed

Some content is hidden

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

117 files changed

+2642
-640
lines changed

Diff for: compiler/rustc_abi/src/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub trait LayoutCalculator {
186186
let (present_first, present_second) = {
187187
let mut present_variants = variants
188188
.iter_enumerated()
189-
.filter_map(|(i, v)| if absent(v) { None } else { Some(i) });
189+
.filter_map(|(i, v)| if !repr.c() && absent(v) { None } else { Some(i) });
190190
(present_variants.next(), present_variants.next())
191191
};
192192
let present_first = match present_first {
@@ -621,7 +621,7 @@ where
621621
let discr_type = repr.discr_type();
622622
let bits = Integer::from_attr(dl, discr_type).size().bits();
623623
for (i, mut val) in discriminants {
624-
if variants[i].iter().any(|f| f.abi.is_uninhabited()) {
624+
if !repr.c() && variants[i].iter().any(|f| f.abi.is_uninhabited()) {
625625
continue;
626626
}
627627
if discr_type.is_signed() {

Diff for: compiler/rustc_abi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ pub enum Variants<FieldIdx: Idx, VariantIdx: Idx> {
14291429
/// Single enum variants, structs/tuples, unions, and all non-ADTs.
14301430
Single { index: VariantIdx },
14311431

1432-
/// Enum-likes with more than one inhabited variant: each variant comes with
1432+
/// Enum-likes with more than one variant: each variant comes with
14331433
/// a *discriminant* (usually the same as the variant index but the user can
14341434
/// assign explicit discriminant values). That discriminant is encoded
14351435
/// as a *tag* on the machine. The layout of each variant is

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -3757,13 +3757,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37573757
assigned_span: Span,
37583758
err_place: Place<'tcx>,
37593759
) {
3760-
let (from_arg, local_decl, local_name) = match err_place.as_local() {
3761-
Some(local) => (
3762-
self.body.local_kind(local) == LocalKind::Arg,
3763-
Some(&self.body.local_decls[local]),
3764-
self.local_names[local],
3765-
),
3766-
None => (false, None, None),
3760+
let (from_arg, local_decl) = match err_place.as_local() {
3761+
Some(local) => {
3762+
(self.body.local_kind(local) == LocalKind::Arg, Some(&self.body.local_decls[local]))
3763+
}
3764+
None => (false, None),
37673765
};
37683766

37693767
// If root local is initialized immediately (everything apart from let
@@ -3795,13 +3793,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37953793
err.span_label(assigned_span, format!("first assignment to {place_description}"));
37963794
}
37973795
if let Some(decl) = local_decl
3798-
&& let Some(name) = local_name
37993796
&& decl.can_be_made_mutable()
38003797
{
3801-
err.span_suggestion(
3802-
decl.source_info.span,
3798+
err.span_suggestion_verbose(
3799+
decl.source_info.span.shrink_to_lo(),
38033800
"consider making this binding mutable",
3804-
format!("mut {name}"),
3801+
"mut ".to_string(),
38053802
Applicability::MachineApplicable,
38063803
);
38073804
if !from_arg
@@ -3813,10 +3810,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
38133810
}))
38143811
)
38153812
{
3816-
err.span_suggestion(
3817-
decl.source_info.span,
3813+
err.span_suggestion_verbose(
3814+
decl.source_info.span.shrink_to_lo(),
38183815
"to modify the original value, take a borrow instead",
3819-
format!("ref mut {name}"),
3816+
"ref mut ".to_string(),
38203817
Applicability::MaybeIncorrect,
38213818
);
38223819
}

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -408,21 +408,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
408408
fn_decl.implicit_self,
409409
hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
410410
) {
411-
err.span_suggestion(
412-
upvar_ident.span,
411+
err.span_suggestion_verbose(
412+
upvar_ident.span.shrink_to_lo(),
413413
"consider changing this to be mutable",
414-
format!("mut {}", upvar_ident.name),
414+
"mut ",
415415
Applicability::MachineApplicable,
416416
);
417417
break;
418418
}
419419
}
420420
}
421421
} else {
422-
err.span_suggestion(
423-
upvar_ident.span,
422+
err.span_suggestion_verbose(
423+
upvar_ident.span.shrink_to_lo(),
424424
"consider changing this to be mutable",
425-
format!("mut {}", upvar_ident.name),
425+
"mut ",
426426
Applicability::MachineApplicable,
427427
);
428428
}
@@ -449,8 +449,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
449449
.is_ok_and(|snippet| snippet.starts_with("&mut ")) =>
450450
{
451451
err.span_label(span, format!("cannot {act}"));
452-
err.span_suggestion(
453-
span,
452+
err.span_suggestion_verbose(
453+
span.with_hi(span.lo() + BytePos(5)),
454454
"try removing `&mut` here",
455455
"",
456456
Applicability::MaybeIncorrect,
@@ -755,13 +755,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
755755
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
756756
..
757757
}) = node
758-
&& let Ok(name) =
759-
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
760758
{
761-
err.span_suggestion(
762-
pat_span,
759+
err.multipart_suggestion(
763760
"consider changing this to be mutable",
764-
format!("&(mut {name})"),
761+
vec![
762+
(pat_span.until(local_decl.source_info.span), "&(mut ".to_string()),
763+
(
764+
local_decl.source_info.span.shrink_to_hi().with_hi(pat_span.hi()),
765+
")".to_string(),
766+
),
767+
],
765768
Applicability::MachineApplicable,
766769
);
767770
return;

Diff for: compiler/rustc_builtin_macros/messages.ftl

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ builtin_macros_asm_expected_other = expected operand, {$is_global_asm ->
1717
*[false] clobber_abi, options
1818
}, or additional template string
1919
20+
builtin_macros_asm_expected_string_literal = expected string literal
21+
.label = not a string literal
22+
2023
builtin_macros_asm_explicit_register_name = explicit register arguments cannot have names
2124
2225
builtin_macros_asm_mayunwind = asm labels are not allowed with the `may_unwind` option
@@ -25,6 +28,8 @@ builtin_macros_asm_modifier_invalid = asm template modifier must be a single cha
2528
2629
builtin_macros_asm_mutually_exclusive = the `{$opt1}` and `{$opt2}` options are mutually exclusive
2730
31+
builtin_macros_asm_no_matched_argument_name = there is no argument named `{$name}`
32+
2833
builtin_macros_asm_noreturn = asm outputs are not allowed with the `noreturn` option
2934
3035
builtin_macros_asm_opt_already_provided = the `{$symbol}` option was already provided
@@ -228,10 +233,16 @@ builtin_macros_only_one_argument = {$name} takes 1 argument
228233
229234
builtin_macros_proc_macro = `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
230235
236+
builtin_macros_proc_macro_attribute_only_be_used_on_bare_functions = the `#[{$path}]` attribute may only be used on bare functions
237+
238+
builtin_macros_proc_macro_attribute_only_usable_with_crate_type = the `#[{$path}]` attribute is only usable with crates of the `proc-macro` crate type
239+
231240
builtin_macros_requires_cfg_pattern =
232241
macro requires a cfg-pattern as an argument
233242
.label = cfg-pattern required
234243
244+
builtin_macros_source_uitls_expected_item = expected item, found `{$token}`
245+
235246
builtin_macros_takes_no_arguments = {$name} takes no arguments
236247
237248
builtin_macros_test_bad_fn = {$kind} functions cannot be used for tests

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,7 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a,
390390
}
391391
Err(opt_lit) => {
392392
let span = opt_lit.map_or(p.token.span, |lit| lit.span);
393-
let mut err = p.dcx().struct_span_err(span, "expected string literal");
394-
err.span_label(span, "not a string literal");
395-
return Err(err);
393+
return Err(p.dcx().create_err(errors::AsmExpectedStringLiteral { span }));
396394
}
397395
};
398396

@@ -639,14 +637,13 @@ fn expand_preparsed_asm(
639637
match args.named_args.get(&Symbol::intern(name)) {
640638
Some(&idx) => Some(idx),
641639
None => {
642-
let msg = format!("there is no argument named `{name}`");
643640
let span = arg.position_span;
644641
ecx.dcx()
645-
.struct_span_err(
646-
template_span
642+
.create_err(errors::AsmNoMatchedArgumentName {
643+
name: name.to_owned(),
644+
span: template_span
647645
.from_inner(InnerSpan::new(span.start, span.end)),
648-
msg,
649-
)
646+
})
650647
.emit();
651648
None
652649
}

Diff for: compiler/rustc_builtin_macros/src/deriving/smart_ptr.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::mem::swap;
33
use ast::HasAttrs;
44
use rustc_ast::{
55
self as ast, GenericArg, GenericBound, GenericParamKind, ItemKind, MetaItem,
6-
TraitBoundModifiers,
6+
TraitBoundModifiers, VariantData,
77
};
8+
use rustc_attr as attr;
89
use rustc_expand::base::{Annotatable, ExtCtxt};
910
use rustc_span::symbol::{sym, Ident};
1011
use rustc_span::Span;
@@ -24,11 +25,43 @@ pub fn expand_deriving_smart_ptr(
2425
_is_const: bool,
2526
) {
2627
let (name_ident, generics) = if let Annotatable::Item(aitem) = item
27-
&& let ItemKind::Struct(_, g) = &aitem.kind
28+
&& let ItemKind::Struct(struct_data, g) = &aitem.kind
2829
{
30+
let is_transparent = aitem.attrs.iter().any(|attr| {
31+
attr::find_repr_attrs(cx.sess, attr)
32+
.into_iter()
33+
.any(|r| matches!(r, attr::ReprTransparent))
34+
});
35+
if !is_transparent {
36+
cx.dcx()
37+
.struct_span_err(
38+
span,
39+
"`SmartPointer` can only be derived on `struct`s with `#[repr(transparent)]`",
40+
)
41+
.emit();
42+
return;
43+
}
44+
if !matches!(
45+
struct_data,
46+
VariantData::Struct { fields, recovered: _ } | VariantData::Tuple(fields, _)
47+
if !fields.is_empty())
48+
{
49+
cx.dcx()
50+
.struct_span_err(
51+
span,
52+
"`SmartPointer` can only be derived on `struct`s with at least one field",
53+
)
54+
.emit();
55+
return;
56+
}
2957
(aitem.ident, g)
3058
} else {
31-
cx.dcx().struct_span_err(span, "`SmartPointer` can only be derived on `struct`s").emit();
59+
cx.dcx()
60+
.struct_span_err(
61+
span,
62+
"`SmartPointer` can only be derived on `struct`s with `#[repr(transparent)]`",
63+
)
64+
.emit();
3265
return;
3366
};
3467

Diff for: compiler/rustc_builtin_macros/src/errors.rs

+40
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,14 @@ pub(crate) struct AsmExpectedComma {
728728
pub(crate) span: Span,
729729
}
730730

731+
#[derive(Diagnostic)]
732+
#[diag(builtin_macros_asm_expected_string_literal)]
733+
pub(crate) struct AsmExpectedStringLiteral {
734+
#[primary_span]
735+
#[label]
736+
pub(crate) span: Span,
737+
}
738+
731739
#[derive(Diagnostic)]
732740
#[diag(builtin_macros_asm_underscore_input)]
733741
pub(crate) struct AsmUnderscoreInput {
@@ -781,6 +789,14 @@ pub(crate) struct AsmNoReturn {
781789
pub(crate) outputs_sp: Vec<Span>,
782790
}
783791

792+
#[derive(Diagnostic)]
793+
#[diag(builtin_macros_asm_no_matched_argument_name)]
794+
pub(crate) struct AsmNoMatchedArgumentName {
795+
pub(crate) name: String,
796+
#[primary_span]
797+
pub(crate) span: Span,
798+
}
799+
784800
#[derive(Diagnostic)]
785801
#[diag(builtin_macros_asm_mayunwind)]
786802
pub(crate) struct AsmMayUnwind {
@@ -872,3 +888,27 @@ pub(crate) struct TakesNoArguments<'a> {
872888
pub span: Span,
873889
pub name: &'a str,
874890
}
891+
892+
#[derive(Diagnostic)]
893+
#[diag(builtin_macros_proc_macro_attribute_only_be_used_on_bare_functions)]
894+
pub(crate) struct AttributeOnlyBeUsedOnBareFunctions<'a> {
895+
#[primary_span]
896+
pub span: Span,
897+
pub path: &'a str,
898+
}
899+
900+
#[derive(Diagnostic)]
901+
#[diag(builtin_macros_proc_macro_attribute_only_usable_with_crate_type)]
902+
pub(crate) struct AttributeOnlyUsableWithCrateType<'a> {
903+
#[primary_span]
904+
pub span: Span,
905+
pub path: &'a str,
906+
}
907+
908+
#[derive(Diagnostic)]
909+
#[diag(builtin_macros_source_uitls_expected_item)]
910+
pub(crate) struct ExpectedItem<'a> {
911+
#[primary_span]
912+
pub span: Span,
913+
pub token: &'a str,
914+
}

Diff for: compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
214214
};
215215

216216
if !is_fn {
217-
let msg = format!(
218-
"the `#[{}]` attribute may only be used on bare functions",
219-
pprust::path_to_string(&attr.get_normal_item().path),
220-
);
221-
222-
self.dcx.span_err(attr.span, msg);
217+
self.dcx
218+
.create_err(errors::AttributeOnlyBeUsedOnBareFunctions {
219+
span: attr.span,
220+
path: &pprust::path_to_string(&attr.get_normal_item().path),
221+
})
222+
.emit();
223223
return;
224224
}
225225

@@ -228,12 +228,12 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
228228
}
229229

230230
if !self.is_proc_macro_crate {
231-
let msg = format!(
232-
"the `#[{}]` attribute is only usable with crates of the `proc-macro` crate type",
233-
pprust::path_to_string(&attr.get_normal_item().path),
234-
);
235-
236-
self.dcx.span_err(attr.span, msg);
231+
self.dcx
232+
.create_err(errors::AttributeOnlyUsableWithCrateType {
233+
span: attr.span,
234+
path: &pprust::path_to_string(&attr.get_normal_item().path),
235+
})
236+
.emit();
237237
return;
238238
}
239239

Diff for: compiler/rustc_builtin_macros/src/source_util.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors;
12
use crate::util::{
23
check_zero_tts, get_single_str_from_tts, get_single_str_spanned_from_tts, parse_expr,
34
};
@@ -165,9 +166,13 @@ pub(crate) fn expand_include<'cx>(
165166
Ok(Some(item)) => ret.push(item),
166167
Ok(None) => {
167168
if self.p.token != token::Eof {
168-
let token = pprust::token_to_string(&self.p.token);
169-
let msg = format!("expected item, found `{token}`");
170-
self.p.dcx().span_err(self.p.token.span, msg);
169+
self.p
170+
.dcx()
171+
.create_err(errors::ExpectedItem {
172+
span: self.p.token.span,
173+
token: &pprust::token_to_string(&self.p.token),
174+
})
175+
.emit();
171176
}
172177

173178
break;

0 commit comments

Comments
 (0)