Skip to content

Commit 820b200

Browse files
Rollup merge of #165725 - RalfJung:target-modifier-error, r=mati865
change target modifier errors to use the diagnostic APIs instead of structs This deduplicates a bunch of strings that were previously duplicated across three strict types.
2 parents 3e26830 + 6fc7f12 commit 820b200

4 files changed

Lines changed: 65 additions & 122 deletions

File tree

‎compiler/rustc_metadata/src/creader.rs‎

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ impl CStore {
380380
}
381381
let extern_crate = data.name();
382382

383+
let emit = |mut diag: rustc_errors::Diag<'_>| {
384+
diag.help(format!("if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch={opt_name}` to silence this {}", diag.level()));
385+
diag.emit();
386+
};
387+
383388
if prefix.is_empty() {
384389
// A synthetic target modifier, does not correspond to an actual flag.
385390
match opt_name.as_str() {
@@ -426,8 +431,7 @@ impl CStore {
426431
let after = if extern_features.len() == 1 { "is" } else { "are" };
427432
diag.help(format!("the target {before} {features} {after} enabled in `{extern_crate}` but disabled in this crate"));
428433
}
429-
diag.help("if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=target-feature` to silence this warning");
430-
diag.emit();
434+
emit(diag);
431435
}
432436
"codegen-backend-name" => {
433437
let diag = format!(
@@ -436,50 +440,79 @@ impl CStore {
436440
let mut diag = tcx.dcx().struct_err(diag);
437441
diag.help("this crate has `-Zllvm-target-feature` set, which means different codegen backends could interpret the target features differently, or ignore them entirely");
438442
diag.help("some target features modify the ABI so Rust crates compiled with different target features cannot be used together safely");
439-
diag.help("if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=codegen-backend-name` to silence this warning");
440-
diag.emit();
443+
emit(diag)
441444
}
442445
_ => panic!("unhandled synthetic target feature {opt_name}"),
443446
}
444447
return;
445448
}
446449

447-
let flag_name = opt_name.clone();
448450
let flag_name_prefixed = format!("-{}{}", prefix, opt_name);
449451

452+
let diag = format!(
453+
"mixing `{flag_name_prefixed}` will cause an ABI mismatch in crate `{local_crate}`"
454+
);
455+
let mut diag = tcx.dcx().struct_err(diag);
456+
diag.help(format!(
457+
"the `{flag_name_prefixed}` flag modifies the ABI so Rust crates compiled \
458+
with different values of this flag cannot be used together safely"
459+
));
460+
450461
match (flag_local_value, flag_extern_value) {
451462
(Some(local_value), Some(extern_value)) => {
452-
tcx.dcx().emit_err(diagnostics::IncompatibleTargetModifiers {
453-
extern_crate,
454-
local_crate,
455-
flag_name,
456-
flag_name_prefixed,
457-
local_value: local_value.to_string(),
458-
extern_value: extern_value.to_string(),
459-
})
463+
diag.note(format!(
464+
"`{flag_name_prefixed}={local_value}` in this crate is incompatible \
465+
with `{flag_name_prefixed}={extern_value}` in dependency `{extern_crate}`"
466+
));
467+
diag.help(format!(
468+
"set `{flag_name_prefixed}={extern_value}` in this crate \
469+
or `{flag_name_prefixed}={local_value}` in `{extern_crate}`"
470+
));
460471
}
461472
(None, Some(extern_value)) => {
462-
tcx.dcx().emit_err(diagnostics::IncompatibleTargetModifiersLMissed {
463-
extern_crate,
464-
local_crate,
465-
flag_name,
466-
flag_name_prefixed,
467-
extern_value: extern_value.to_string(),
468-
has_extern_value: !extern_value.is_empty(),
469-
})
473+
let has_extern_value = !extern_value.is_empty();
474+
diag.note(format!(
475+
"`{flag_name_prefixed}` is unset in this crate which is \
476+
incompatible with {} in dependency `{extern_crate}`",
477+
if has_extern_value {
478+
format!("`{flag_name_prefixed}={extern_value}`")
479+
} else {
480+
format!("`{flag_name_prefixed}` being set")
481+
}
482+
));
483+
diag.help(format!(
484+
"set {} in this crate or unset `{flag_name_prefixed}` in `{extern_crate}`",
485+
if has_extern_value {
486+
format!("`{flag_name_prefixed}={extern_value}`")
487+
} else {
488+
format!("`{flag_name_prefixed}`")
489+
}
490+
));
470491
}
471492
(Some(local_value), None) => {
472-
tcx.dcx().emit_err(diagnostics::IncompatibleTargetModifiersRMissed {
473-
extern_crate,
474-
local_crate,
475-
flag_name,
476-
flag_name_prefixed,
477-
local_value: local_value.to_string(),
478-
has_local_value: !local_value.is_empty(),
479-
})
493+
let has_local_value = !local_value.is_empty();
494+
diag.note(format!(
495+
"{} in this crate is incompatible with \
496+
`{flag_name_prefixed}` being unset in dependency `{extern_crate}`",
497+
if has_local_value {
498+
format!("`{flag_name_prefixed}={local_value}`")
499+
} else {
500+
format!("`{flag_name_prefixed}` being set")
501+
}
502+
));
503+
diag.help(format!(
504+
"unset `{flag_name_prefixed}` in this crate or set {} in `{extern_crate}`",
505+
if has_local_value {
506+
format!("`{flag_name_prefixed}={local_value}`")
507+
} else {
508+
format!("`{flag_name_prefixed}`")
509+
}
510+
));
480511
}
481512
(None, None) => panic!("Incorrect target modifiers report_diff(None, None)"),
482513
};
514+
515+
emit(diag);
483516
};
484517
let mut it1 = mods.iter().map(tmod_extender);
485518
let mut it2 = dep_mods.iter().map(tmod_extender);

‎compiler/rustc_metadata/src/diagnostics.rs‎

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -577,96 +577,6 @@ pub(crate) struct WasmCAbi {
577577
pub span: Span,
578578
}
579579

580-
#[derive(Diagnostic)]
581-
#[diag("mixing `{$flag_name_prefixed}` will cause an ABI mismatch in crate `{$local_crate}`")]
582-
#[help(
583-
"the `{$flag_name_prefixed}` flag modifies the ABI so Rust crates compiled with different \
584-
values of this flag cannot be used together safely"
585-
)]
586-
#[note(
587-
"`{$flag_name_prefixed}={$local_value}` in this crate is incompatible with \
588-
`{$flag_name_prefixed}={$extern_value}` in dependency `{$extern_crate}`"
589-
)]
590-
#[help(
591-
"set `{$flag_name_prefixed}={$extern_value}` in this crate or \
592-
`{$flag_name_prefixed}={$local_value}` in `{$extern_crate}`"
593-
)]
594-
#[help(
595-
"if you are sure this will not cause problems, you may use \
596-
`-Cunsafe-allow-abi-mismatch={$flag_name}` to silence this error"
597-
)]
598-
pub(crate) struct IncompatibleTargetModifiers {
599-
pub extern_crate: Symbol,
600-
pub local_crate: Symbol,
601-
pub flag_name: String,
602-
pub flag_name_prefixed: String,
603-
pub local_value: String,
604-
pub extern_value: String,
605-
}
606-
607-
#[derive(Diagnostic)]
608-
#[diag("mixing `{$flag_name_prefixed}` will cause an ABI mismatch in crate `{$local_crate}`")]
609-
#[help(
610-
"the `{$flag_name_prefixed}` flag modifies the ABI so Rust crates compiled with different \
611-
values of this flag cannot be used together safely"
612-
)]
613-
#[note(
614-
"`{$flag_name_prefixed}` is unset in this crate which is incompatible with {$has_extern_value ->
615-
[false] `{$flag_name_prefixed}` being set
616-
*[other] `{$flag_name_prefixed}={$extern_value}`
617-
} in dependency `{$extern_crate}`"
618-
)]
619-
#[help(
620-
"set {$has_extern_value ->
621-
[false] `{$flag_name_prefixed}`
622-
*[other] `{$flag_name_prefixed}={$extern_value}`
623-
} in this crate or unset `{$flag_name_prefixed}` in `{$extern_crate}`"
624-
)]
625-
#[help(
626-
"if you are sure this will not cause problems, you may use \
627-
`-Cunsafe-allow-abi-mismatch={$flag_name}` to silence this error"
628-
)]
629-
pub(crate) struct IncompatibleTargetModifiersLMissed {
630-
pub extern_crate: Symbol,
631-
pub local_crate: Symbol,
632-
pub flag_name: String,
633-
pub flag_name_prefixed: String,
634-
pub extern_value: String,
635-
pub has_extern_value: bool,
636-
}
637-
638-
#[derive(Diagnostic)]
639-
#[diag("mixing `{$flag_name_prefixed}` will cause an ABI mismatch in crate `{$local_crate}`")]
640-
#[help(
641-
"the `{$flag_name_prefixed}` flag modifies the ABI so Rust crates compiled with different \
642-
values of this flag cannot be used together safely"
643-
)]
644-
#[note(
645-
"{$has_local_value ->
646-
[false] `{$flag_name_prefixed}` being set
647-
*[other] `{$flag_name_prefixed}={$local_value}`
648-
} in this crate is incompatible with `{$flag_name_prefixed}` being unset in dependency \
649-
`{$extern_crate}`"
650-
)]
651-
#[help(
652-
"unset `{$flag_name_prefixed}` in this crate or set {$has_local_value ->
653-
[false] `{$flag_name_prefixed}`
654-
*[other] `{$flag_name_prefixed}={$local_value}`
655-
} in `{$extern_crate}`"
656-
)]
657-
#[help(
658-
"if you are sure this will not cause problems, you may use \
659-
`-Cunsafe-allow-abi-mismatch={$flag_name}` to silence this error"
660-
)]
661-
pub(crate) struct IncompatibleTargetModifiersRMissed {
662-
pub extern_crate: Symbol,
663-
pub local_crate: Symbol,
664-
pub flag_name: String,
665-
pub flag_name_prefixed: String,
666-
pub local_value: String,
667-
pub has_local_value: bool,
668-
}
669-
670580
#[derive(Diagnostic)]
671581
#[diag(
672582
"unknown target modifier `{$flag_name}`, requested by \

‎compiler/rustc_session/src/options.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub struct ExtendedTargetModifierInfo {
7070
/// Flag prefix (usually, 'C' for codegen flags or 'Z' for unstable flags). And empty string
7171
/// indicates a synthetic target modifier, which does not correspond directly to a flag.
7272
pub prefix: String,
73-
/// Flag name. For synthetic features, this is still relevant as the name used to suppress
74-
/// the error for mismatches involving this target modifier.
73+
/// Flag name. For synthetic target modifiers, this is still relevant as the name is used with
74+
/// `-Cunsafe-allow-abi-mismatch`.
7575
pub name: String,
7676
/// Flag parsed technical value
7777
pub tech_value: String,

‎tests/ui-fulldeps/codegen-backend/llvm-target-features.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: mixing codegen backends while using backend-specific target features may
22
|
33
= help: this crate has `-Zllvm-target-feature` set, which means different codegen backends could interpret the target features differently, or ignore them entirely
44
= help: some target features modify the ABI so Rust crates compiled with different target features cannot be used together safely
5-
= help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=codegen-backend-name` to silence this warning
5+
= help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=codegen-backend-name` to silence this error
66

77
error: aborting due to 1 previous error
88

0 commit comments

Comments
 (0)