Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub(crate) enum AssocItemNotFoundLabel<'a> {
NotFound {
#[primary_span]
span: Span,
assoc_ident: Ident,
assoc_kind: &'static str,
},
#[label(
"there is {$identically_named ->
Expand Down Expand Up @@ -149,6 +151,7 @@ pub(crate) enum AssocItemNotFoundSugg<'a> {
trait_ref: String,
suggested_name: Symbol,
identically_named: bool,
assoc_kind: &'static str,
#[applicability]
applicability: Applicability,
},
Expand Down
22 changes: 14 additions & 8 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}

let assoc_kind_str = assoc_tag_str(assoc_tag);
let assoc_kind = assoc_tag_str(assoc_tag);
let qself_str = qself.to_string(tcx);

// The fallback span is needed because `assoc_name` might be an `Fn()`'s `Output` without a
Expand All @@ -151,7 +151,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let mut err = errors::AssocItemNotFound {
span: if is_dummy { span } else { assoc_ident.span },
assoc_ident,
assoc_kind: assoc_kind_str,
assoc_kind,
qself: &qself_str,
label: None,
sugg: None,
Expand All @@ -161,7 +161,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
};

if is_dummy {
err.label = Some(errors::AssocItemNotFoundLabel::NotFound { span });
err.label =
Some(errors::AssocItemNotFoundLabel::NotFound { span, assoc_ident, assoc_kind });
return self.dcx().emit_err(err);
}

Expand All @@ -181,7 +182,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
{
err.sugg = Some(errors::AssocItemNotFoundSugg::Similar {
span: assoc_ident.span,
assoc_kind: assoc_kind_str,
assoc_kind,
suggested_name,
});
return self.dcx().emit_err(err);
Expand Down Expand Up @@ -224,7 +225,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let trait_name = tcx.def_path_str(best_trait);
err.label = Some(errors::AssocItemNotFoundLabel::FoundInOtherTrait {
span: assoc_ident.span,
assoc_kind: assoc_kind_str,
assoc_kind,
trait_name: &trait_name,
suggested_name,
identically_named: suggested_name == assoc_ident.name,
Expand Down Expand Up @@ -256,7 +257,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
err.sugg = Some(errors::AssocItemNotFoundSugg::SimilarInOtherTrait {
span: assoc_ident.span,
trait_name: &trait_name,
assoc_kind: assoc_kind_str,
assoc_kind,
suggested_name,
});
return self.dcx().emit_err(err);
Expand Down Expand Up @@ -286,6 +287,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
trait_ref,
identically_named,
suggested_name,
assoc_kind,
applicability,
});
} else {
Expand Down Expand Up @@ -322,11 +324,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
err.sugg = Some(errors::AssocItemNotFoundSugg::Other {
span: assoc_ident.span,
qself: &qself_str,
assoc_kind: assoc_kind_str,
assoc_kind,
suggested_name: *candidate_name,
});
} else {
err.label = Some(errors::AssocItemNotFoundLabel::NotFound { span: assoc_ident.span });
err.label = Some(errors::AssocItemNotFoundLabel::NotFound {
span: assoc_ident.span,
assoc_ident,
assoc_kind,
});
}

self.dcx().emit_err(err)
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2320,8 +2320,9 @@ impl EarlyLintPass for IncompleteInternalFeatures {
if features.incomplete(name) {
let note = rustc_feature::find_feature_issue(name, GateIssue::Language)
.map(|n| BuiltinFeatureIssueNote { n });
let help =
HAS_MIN_FEATURES.contains(&name).then_some(BuiltinIncompleteFeaturesHelp);
let help = HAS_MIN_FEATURES
.contains(&name)
.then_some(BuiltinIncompleteFeaturesHelp { name });

cx.emit_span_lint(
INCOMPLETE_FEATURES,
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_lint/src/deref_into_dyn_supertrait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
ty::AssocTag::Type,
item.owner_id.to_def_id(),
)
.map(|label| SupertraitAsDerefTargetLabel { label: tcx.def_span(label.def_id) });
.map(|label| SupertraitAsDerefTargetLabel {
label: tcx.def_span(label.def_id),
self_ty,
});
let span = tcx.def_span(item.owner_id.def_id);
cx.emit_span_lint(
DEREF_INTO_DYN_SUPERTRAIT,
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@ pub fn decorate_builtin_lint(
BuiltinLintDiag::RedundantImport(spans, ident) => {
let subs = spans
.into_iter()
.map(|(span, is_imported)| {
(match (span.is_dummy(), is_imported) {
(false, true) => lints::RedundantImportSub::ImportedHere,
(false, false) => lints::RedundantImportSub::DefinedHere,
(true, true) => lints::RedundantImportSub::ImportedPrelude,
(true, false) => lints::RedundantImportSub::DefinedPrelude,
})(span)
.map(|(span, is_imported)| match (span.is_dummy(), is_imported) {
(false, true) => lints::RedundantImportSub::ImportedHere { span, ident },
(false, false) => lints::RedundantImportSub::DefinedHere { span, ident },
(true, true) => lints::RedundantImportSub::ImportedPrelude { span, ident },
(true, false) => lints::RedundantImportSub::DefinedPrelude { span, ident },
})
.collect();
lints::RedundantImport { subs, ident }.decorate_lint(diag);
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/interior_mutable_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ impl<'tcx> LateLintPass<'tcx> for InteriorMutableConsts {
Some(ConstItemInteriorMutationsSuggestionStatic::Spanful {
const_: const_item.vis_span.between(ident.span),
before: if !vis_span.is_empty() { " " } else { "" },
const_name,
})
} else {
Some(ConstItemInteriorMutationsSuggestionStatic::Spanless)
Some(ConstItemInteriorMutationsSuggestionStatic::Spanless { const_name })
}
} else {
None
Expand Down
49 changes: 33 additions & 16 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,9 @@ pub(crate) struct BuiltinInternalFeatures {

#[derive(Subdiagnostic)]
#[help("consider using `min_{$name}` instead, which is more stable and complete")]
pub(crate) struct BuiltinIncompleteFeaturesHelp;
pub(crate) struct BuiltinIncompleteFeaturesHelp {
pub name: Symbol,
}

#[derive(Subdiagnostic)]
#[note("see issue #{$n} <https://github.com/rust-lang/rust/issues/{$n}> for more information")]
Expand Down Expand Up @@ -663,14 +665,15 @@ pub(crate) struct SupertraitAsDerefTarget<'a> {
)]
pub label: Span,
#[subdiagnostic]
pub label2: Option<SupertraitAsDerefTargetLabel>,
pub label2: Option<SupertraitAsDerefTargetLabel<'a>>,
}

#[derive(Subdiagnostic)]
#[label("target type is a supertrait of `{$self_ty}`")]
pub(crate) struct SupertraitAsDerefTargetLabel {
pub(crate) struct SupertraitAsDerefTargetLabel<'a> {
#[primary_span]
pub label: Span,
pub self_ty: Ty<'a>,
}

// enum_intrinsics_non_enums.rs
Expand Down Expand Up @@ -958,9 +961,10 @@ pub(crate) enum ConstItemInteriorMutationsSuggestionStatic {
#[primary_span]
const_: Span,
before: &'static str,
const_name: Ident,
},
#[help("for a shared instance of `{$const_name}`, consider making it a `static` item instead")]
Spanless,
Spanless { const_name: Ident },
}

// reference_casting.rs
Expand Down Expand Up @@ -1972,26 +1976,23 @@ pub(crate) enum UseInclusiveRange<'a> {
#[diag("literal out of range for `{$ty}`")]
pub(crate) struct OverflowingBinHex<'a> {
pub ty: &'a str,
pub lit: String,
pub dec: u128,
pub actually: String,
#[subdiagnostic]
pub sign: OverflowingBinHexSign,
pub sign: OverflowingBinHexSign<'a>,
#[subdiagnostic]
pub sub: Option<OverflowingBinHexSub<'a>>,
#[subdiagnostic]
pub sign_bit_sub: Option<OverflowingBinHexSignBitSub<'a>>,
}

#[derive(Subdiagnostic)]
pub(crate) enum OverflowingBinHexSign {
pub(crate) enum OverflowingBinHexSign<'a> {
#[note(
"the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}` and will become `{$actually}{$ty}`"
)]
Positive,
Positive { lit: String, ty: &'a str, actually: String, dec: u128 },
#[note("the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`")]
#[note("and the value `-{$lit}` will become `{$actually}{$ty}`")]
Negative,
Negative { lit: String, ty: &'a str, actually: String, dec: u128 },
}

#[derive(Subdiagnostic)]
Expand Down Expand Up @@ -2562,6 +2563,7 @@ pub(crate) struct UnusedDelimSuggestion {
#[suggestion_part(code = "{end_replace}")]
pub end_span: Span,
pub end_replace: &'static str,
pub delim: &'static str,
}

#[derive(Diagnostic)]
Expand Down Expand Up @@ -3131,20 +3133,35 @@ pub(crate) enum UnusedImportsSugg {
pub(crate) struct RedundantImport {
#[subdiagnostic]
pub subs: Vec<RedundantImportSub>,

pub ident: Ident,
}

#[derive(Subdiagnostic)]
pub(crate) enum RedundantImportSub {
#[label("the item `{$ident}` is already imported here")]
ImportedHere(#[primary_span] Span),
ImportedHere {
#[primary_span]
span: Span,
ident: Ident,
},
#[label("the item `{$ident}` is already defined here")]
DefinedHere(#[primary_span] Span),
DefinedHere {
#[primary_span]
span: Span,
ident: Ident,
},
#[label("the item `{$ident}` is already imported by the extern prelude")]
ImportedPrelude(#[primary_span] Span),
ImportedPrelude {
#[primary_span]
span: Span,
ident: Ident,
},
#[label("the item `{$ident}` is already defined by the extern prelude")]
DefinedPrelude(#[primary_span] Span),
DefinedPrelude {
#[primary_span]
span: Span,
ident: Ident,
},
}

#[derive(LintDiagnostic)]
Expand Down
29 changes: 17 additions & 12 deletions compiler/rustc_lint/src/types/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,21 @@ fn report_bin_hex_error(
(t.name_str(), actually.to_string())
}
};
let sign =
if negative { OverflowingBinHexSign::Negative } else { OverflowingBinHexSign::Positive };
let sign = if negative {
OverflowingBinHexSign::Negative {
lit: repr_str.clone(),
dec: val,
actually: actually.clone(),
ty: t,
}
} else {
OverflowingBinHexSign::Positive {
lit: repr_str.clone(),
dec: val,
actually: actually.clone(),
ty: t,
}
};
let sub = get_type_suggestion(cx.typeck_results().node_type(hir_id), val, negative).map(
|suggestion_ty| {
if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
Expand Down Expand Up @@ -194,7 +207,7 @@ fn report_bin_hex_error(
Some(OverflowingBinHexSignBitSub {
span,
lit_no_suffix,
negative_val: actually.clone(),
negative_val: actually,
int_ty: int_ty.name_str(),
uint_ty: Integer::fit_unsigned(val).uint_ty_str(),
})
Expand All @@ -204,15 +217,7 @@ fn report_bin_hex_error(
cx.emit_span_lint(
OVERFLOWING_LITERALS,
span,
OverflowingBinHex {
ty: t,
lit: repr_str.clone(),
dec: val,
actually,
sign,
sub,
sign_bit_sub,
},
OverflowingBinHex { ty: t, sign, sub, sign_bit_sub },
)
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ trait UnusedDelimLint {
start_replace: lo_replace,
end_span: hi,
end_replace: hi_replace,
delim: Self::DELIM_STR,
}
});
cx.emit_span_lint(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_macros/src/diagnostics/subdiagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
let mut calls = TokenStream::new();
for (kind, messages) in kind_messages {
let message = format_ident!("__message");
let message_stream = messages.diag_message(None);
let message_stream = messages.diag_message(Some(self.variant));
calls.extend(quote! { let #message = #diag.eagerly_translate(#message_stream); });

let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn add_library(
crate_name: tcx.crate_name(cnum),
non_static_deps: unavailable_as_static
.drain(..)
.map(|cnum| NonStaticCrateDep { crate_name_: tcx.crate_name(cnum) })
.map(|cnum| NonStaticCrateDep { sub_crate_name: tcx.crate_name(cnum) })
.collect(),
rustc_driver_help: linking_to_rustc_driver,
});
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ pub struct CrateDepMultiple {
}

#[derive(Subdiagnostic)]
#[note("`{$crate_name}` was unavailable as a static crate, preventing fully static linking")]
#[note("`{$sub_crate_name}` was unavailable as a static crate, preventing fully static linking")]
pub struct NonStaticCrateDep {
/// It's different from `crate_name` in main Diagnostic.
pub crate_name_: Symbol,
pub sub_crate_name: Symbol,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a bug! The message should have referred to crate_name_.
I don't think there's an easy way to write a regression test for this, but this shows that this PR is doing good

}

#[derive(Diagnostic)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
CallToDeprecatedSafeFnRequiresUnsafe {
span,
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
guarantee,
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
start_of_line_suggestion: suggestion,
start_of_line: sm.span_extend_to_line(span).shrink_to_lo(),
left: span.shrink_to_lo(),
right: span.shrink_to_hi(),
guarantee,
},
},
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafe {
#[label("call to unsafe function")]
pub(crate) span: Span,
pub(crate) function: String,
pub(crate) guarantee: String,
#[subdiagnostic]
pub(crate) sub: CallToDeprecatedSafeFnRequiresUnsafeSub,
}
Expand All @@ -33,6 +32,7 @@ pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafeSub {
pub(crate) left: Span,
#[suggestion_part(code = " }}")]
pub(crate) right: Span,
pub(crate) guarantee: String,
}

#[derive(Diagnostic)]
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_transform/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,5 @@ pub(crate) struct ForceInlineFailure {
#[note("`{$callee}` is required to be inlined to: {$sym}")]
pub(crate) struct ForceInlineJustification {
pub sym: Symbol,
pub callee: String,
}
Loading
Loading