Skip to content

Commit 109e5e5

Browse files
committed
Move early buffered lint ambigous-glob-imports to a dyn lint diagnostic
1 parent 3ad6359 commit 109e5e5

File tree

6 files changed

+61
-77
lines changed

6 files changed

+61
-77
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,22 +2049,6 @@ pub fn elided_lifetime_in_path_suggestion(
20492049
ElidedLifetimeInPathSubdiag { expected, indicate }
20502050
}
20512051

2052-
pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
2053-
diag: &mut Diag<'a, G>,
2054-
ambiguity: rustc_lint_defs::AmbiguityErrorDiag,
2055-
) {
2056-
diag.span_label(ambiguity.label_span, ambiguity.label_msg);
2057-
diag.note(ambiguity.note_msg);
2058-
diag.span_note(ambiguity.b1_span, ambiguity.b1_note_msg);
2059-
for help_msg in ambiguity.b1_help_msgs {
2060-
diag.help(help_msg);
2061-
}
2062-
diag.span_note(ambiguity.b2_span, ambiguity.b2_note_msg);
2063-
for help_msg in ambiguity.b2_help_msgs {
2064-
diag.help(help_msg);
2065-
}
2066-
}
2067-
20682052
/// Grammatical tool for displaying messages to end users in a nice form.
20692053
///
20702054
/// Returns "an" if the given string starts with a vowel, and "a" otherwise.

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,6 @@ pub fn decorate_builtin_lint(
251251
}
252252
.decorate_lint(diag);
253253
}
254-
BuiltinLintDiag::AmbiguousGlobImports { diag: ambiguity } => {
255-
lints::AmbiguousGlobImports { ambiguity }.decorate_lint(diag);
256-
}
257254
BuiltinLintDiag::AmbiguousGlobReexports {
258255
name,
259256
namespace,

compiler/rustc_lint/src/lints.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_macros::{LintDiagnostic, Subdiagnostic};
1515
use rustc_middle::ty::inhabitedness::InhabitedPredicate;
1616
use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt};
1717
use rustc_session::Session;
18-
use rustc_session::lint::AmbiguityErrorDiag;
1918
use rustc_span::edition::Edition;
2019
use rustc_span::{Ident, Span, Symbol, sym};
2120

@@ -2836,18 +2835,6 @@ pub(crate) struct NamedArgumentUsedPositionally {
28362835
pub named_arg_name: String,
28372836
}
28382837

2839-
// FIXME: make this translatable
2840-
pub(crate) struct AmbiguousGlobImports {
2841-
pub ambiguity: AmbiguityErrorDiag,
2842-
}
2843-
2844-
impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for AmbiguousGlobImports {
2845-
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, G>) {
2846-
diag.primary_message(self.ambiguity.msg.clone());
2847-
rustc_errors::report_ambiguity_error(diag, self.ambiguity);
2848-
}
2849-
}
2850-
28512838
#[derive(LintDiagnostic)]
28522839
#[diag(lint_ambiguous_glob_reexport)]
28532840
pub(crate) struct AmbiguousGlobReexports {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -593,21 +593,6 @@ impl StableCompare for LintId {
593593
}
594594
}
595595

596-
#[derive(Debug)]
597-
pub struct AmbiguityErrorDiag {
598-
pub msg: String,
599-
pub span: Span,
600-
pub label_span: Span,
601-
pub label_msg: String,
602-
pub note_msg: String,
603-
pub b1_span: Span,
604-
pub b1_note_msg: String,
605-
pub b1_help_msgs: Vec<String>,
606-
pub b2_span: Span,
607-
pub b2_note_msg: String,
608-
pub b2_help_msgs: Vec<String>,
609-
}
610-
611596
#[derive(Debug, Clone)]
612597
pub enum DeprecatedSinceKind {
613598
InEffect,
@@ -678,9 +663,6 @@ pub enum BuiltinLintDiag {
678663
/// Indicates if the named argument is used as a width/precision for formatting
679664
is_formatting_arg: bool,
680665
},
681-
AmbiguousGlobImports {
682-
diag: AmbiguityErrorDiag,
683-
},
684666
AmbiguousGlobReexports {
685667
/// The name for which collision(s) have occurred.
686668
name: String,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::unord::{UnordMap, UnordSet};
1111
use rustc_errors::codes::*;
1212
use rustc_errors::{
1313
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
14-
report_ambiguity_error, struct_span_code_err,
14+
struct_span_code_err,
1515
};
1616
use rustc_feature::BUILTIN_ATTRIBUTES;
1717
use rustc_hir::attrs::{AttributeKind, CfgEntry, StrippedCfgItem};
@@ -22,16 +22,16 @@ use rustc_hir::{PrimTy, Stability, StabilityLevel, find_attr};
2222
use rustc_middle::bug;
2323
use rustc_middle::ty::TyCtxt;
2424
use rustc_session::Session;
25+
use rustc_session::lint::BuiltinLintDiag;
2526
use rustc_session::lint::builtin::{
2627
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS,
2728
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
2829
};
29-
use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
3030
use rustc_session::utils::was_invoked_from_cargo;
3131
use rustc_span::edit_distance::find_best_match_for_name;
3232
use rustc_span::edition::Edition;
3333
use rustc_span::hygiene::MacroKind;
34-
use rustc_span::source_map::SourceMap;
34+
use rustc_span::source_map::{SourceMap, Spanned};
3535
use rustc_span::{BytePos, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym};
3636
use thin_vec::{ThinVec, thin_vec};
3737
use tracing::{debug, instrument};
@@ -145,21 +145,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
145145
}
146146

147147
for ambiguity_error in &self.ambiguity_errors {
148-
let diag = self.ambiguity_diagnostics(ambiguity_error);
148+
let diag = self.ambiguity_diagnostic(ambiguity_error);
149+
149150
if ambiguity_error.warning {
150151
let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
151152
unreachable!()
152153
};
153154
self.lint_buffer.buffer_lint(
154155
AMBIGUOUS_GLOB_IMPORTS,
155156
import.root_id,
156-
ambiguity_error.ident.span,
157-
BuiltinLintDiag::AmbiguousGlobImports { diag },
157+
diag.ident.span,
158+
diag,
158159
);
159160
} else {
160-
let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", diag.msg);
161-
report_ambiguity_error(&mut err, diag);
162-
err.emit();
161+
self.dcx().emit_err(diag);
163162
}
164163
}
165164

@@ -1995,7 +1994,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19951994
}
19961995
}
19971996

1998-
fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'ra>) -> AmbiguityErrorDiag {
1997+
fn ambiguity_diagnostic(&self, ambiguity_error: &AmbiguityError<'ra>) -> errors::Ambiguity {
19991998
let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error;
20001999
let extern_prelude_ambiguity = || {
20012000
self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| {
@@ -2038,8 +2037,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20382037
}
20392038

20402039
(
2041-
b.span,
2042-
note_msg,
2040+
Spanned { node: note_msg, span: b.span },
20432041
help_msgs
20442042
.iter()
20452043
.enumerate()
@@ -2050,20 +2048,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20502048
.collect::<Vec<_>>(),
20512049
)
20522050
};
2053-
let (b1_span, b1_note_msg, b1_help_msgs) = could_refer_to(b1, misc1, "");
2054-
let (b2_span, b2_note_msg, b2_help_msgs) = could_refer_to(b2, misc2, " also");
2055-
2056-
AmbiguityErrorDiag {
2057-
msg: format!("`{ident}` is ambiguous"),
2058-
span: ident.span,
2059-
label_span: ident.span,
2060-
label_msg: "ambiguous name".to_string(),
2061-
note_msg: format!("ambiguous because of {}", kind.descr()),
2062-
b1_span,
2063-
b1_note_msg,
2051+
let (b1_note, b1_help_msgs) = could_refer_to(b1, misc1, "");
2052+
let (b2_note, b2_help_msgs) = could_refer_to(b2, misc2, " also");
2053+
2054+
errors::Ambiguity {
2055+
ident,
2056+
kind: kind.descr(),
2057+
b1_note,
20642058
b1_help_msgs,
2065-
b2_span,
2066-
b2_note_msg,
2059+
b2_note,
20672060
b2_help_msgs,
20682061
}
20692062
}

compiler/rustc_resolve/src/errors.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc_errors::codes::*;
22
use rustc_errors::{
3-
Applicability, Diag, DiagMessage, ElidedLifetimeInPathSubdiag, EmissionGuarantee, IntoDiagArg,
4-
LintDiagnostic, MultiSpan, Subdiagnostic,
3+
Applicability, Diag, DiagCtxtHandle, DiagMessage, Diagnostic, ElidedLifetimeInPathSubdiag,
4+
EmissionGuarantee, IntoDiagArg, Level, LintDiagnostic, MultiSpan, Subdiagnostic,
55
};
66
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
7+
use rustc_span::source_map::Spanned;
78
use rustc_span::{Ident, Span, Symbol};
89

910
use crate::late::PatternSource;
@@ -1445,3 +1446,43 @@ pub(crate) struct UnknownDiagnosticAttributeTypoSugg {
14451446
pub span: Span,
14461447
pub typo_name: Symbol,
14471448
}
1449+
1450+
// FIXME: Make this properly translatable.
1451+
pub(crate) struct Ambiguity {
1452+
pub ident: Ident,
1453+
pub kind: &'static str,
1454+
pub b1_note: Spanned<String>,
1455+
pub b1_help_msgs: Vec<String>,
1456+
pub b2_note: Spanned<String>,
1457+
pub b2_help_msgs: Vec<String>,
1458+
}
1459+
1460+
impl Ambiguity {
1461+
fn decorate<'a>(self, diag: &mut Diag<'a, impl EmissionGuarantee>) {
1462+
diag.primary_message(format!("`{}` is ambiguous", self.ident));
1463+
diag.span_label(self.ident.span, "ambiguous name");
1464+
diag.note(format!("ambiguous because of {}", self.kind));
1465+
diag.span_note(self.b1_note.span, self.b1_note.node);
1466+
for help_msg in self.b1_help_msgs {
1467+
diag.help(help_msg);
1468+
}
1469+
diag.span_note(self.b2_note.span, self.b2_note.node);
1470+
for help_msg in self.b2_help_msgs {
1471+
diag.help(help_msg);
1472+
}
1473+
}
1474+
}
1475+
1476+
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Ambiguity {
1477+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
1478+
let mut diag = Diag::new(dcx, level, "").with_span(self.ident.span).with_code(E0659);
1479+
self.decorate(&mut diag);
1480+
diag
1481+
}
1482+
}
1483+
1484+
impl<'a> LintDiagnostic<'a, ()> for Ambiguity {
1485+
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
1486+
self.decorate(diag);
1487+
}
1488+
}

0 commit comments

Comments
 (0)