Skip to content

Commit 1a4e8e8

Browse files
committed
Move early buffered lint ambigous-glob-imports to a dyn lint diagnostic
1 parent 7ec1804 commit 1a4e8e8

21 files changed

+113
-114
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,22 +2035,6 @@ pub fn elided_lifetime_in_path_suggestion(
20352035
ElidedLifetimeInPathSubdiag { expected, indicate }
20362036
}
20372037

2038-
pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
2039-
diag: &mut Diag<'a, G>,
2040-
ambiguity: rustc_lint_defs::AmbiguityErrorDiag,
2041-
) {
2042-
diag.span_label(ambiguity.label_span, ambiguity.label_msg);
2043-
diag.note(ambiguity.note_msg);
2044-
diag.span_note(ambiguity.b1_span, ambiguity.b1_note_msg);
2045-
for help_msg in ambiguity.b1_help_msgs {
2046-
diag.help(help_msg);
2047-
}
2048-
diag.span_note(ambiguity.b2_span, ambiguity.b2_note_msg);
2049-
for help_msg in ambiguity.b2_help_msgs {
2050-
diag.help(help_msg);
2051-
}
2052-
}
2053-
20542038
/// Grammatical tool for displaying messages to end users in a nice form.
20552039
///
20562040
/// 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

@@ -2796,18 +2795,6 @@ pub(crate) struct NamedArgumentUsedPositionally {
27962795
pub named_arg_name: String,
27972796
}
27982797

2799-
// FIXME: make this translatable
2800-
pub(crate) struct AmbiguousGlobImports {
2801-
pub ambiguity: AmbiguityErrorDiag,
2802-
}
2803-
2804-
impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for AmbiguousGlobImports {
2805-
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, G>) {
2806-
diag.primary_message(self.ambiguity.msg.clone());
2807-
rustc_errors::report_ambiguity_error(diag, self.ambiguity);
2808-
}
2809-
}
2810-
28112798
#[derive(LintDiagnostic)]
28122799
#[diag(lint_ambiguous_glob_reexport)]
28132800
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
@@ -9,7 +9,7 @@ use rustc_data_structures::unord::{UnordMap, UnordSet};
99
use rustc_errors::codes::*;
1010
use rustc_errors::{
1111
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
12-
report_ambiguity_error, struct_span_code_err,
12+
struct_span_code_err,
1313
};
1414
use rustc_feature::BUILTIN_ATTRIBUTES;
1515
use rustc_hir::attrs::{AttributeKind, CfgEntry, StrippedCfgItem};
@@ -20,16 +20,16 @@ use rustc_hir::{PrimTy, Stability, StabilityLevel, find_attr};
2020
use rustc_middle::bug;
2121
use rustc_middle::ty::TyCtxt;
2222
use rustc_session::Session;
23+
use rustc_session::lint::BuiltinLintDiag;
2324
use rustc_session::lint::builtin::{
2425
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS,
2526
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
2627
};
27-
use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
2828
use rustc_session::utils::was_invoked_from_cargo;
2929
use rustc_span::edit_distance::find_best_match_for_name;
3030
use rustc_span::edition::Edition;
3131
use rustc_span::hygiene::MacroKind;
32-
use rustc_span::source_map::SourceMap;
32+
use rustc_span::source_map::{SourceMap, Spanned};
3333
use rustc_span::{BytePos, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym};
3434
use thin_vec::{ThinVec, thin_vec};
3535
use tracing::{debug, instrument};
@@ -143,21 +143,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
143143
}
144144

145145
for ambiguity_error in &self.ambiguity_errors {
146-
let diag = self.ambiguity_diagnostics(ambiguity_error);
146+
let diag = self.ambiguity_diagnostic(ambiguity_error);
147+
147148
if ambiguity_error.warning {
148149
let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
149150
unreachable!()
150151
};
151152
self.lint_buffer.buffer_lint(
152153
AMBIGUOUS_GLOB_IMPORTS,
153154
import.root_id,
154-
ambiguity_error.ident.span,
155-
BuiltinLintDiag::AmbiguousGlobImports { diag },
155+
diag.ident.span,
156+
diag,
156157
);
157158
} else {
158-
let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", diag.msg);
159-
report_ambiguity_error(&mut err, diag);
160-
err.emit();
159+
self.dcx().emit_err(diag);
161160
}
162161
}
163162

@@ -1982,7 +1981,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19821981
}
19831982
}
19841983

1985-
fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'ra>) -> AmbiguityErrorDiag {
1984+
fn ambiguity_diagnostic(&self, ambiguity_error: &AmbiguityError<'ra>) -> errors::Ambiguity {
19861985
let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error;
19871986
let extern_prelude_ambiguity = || {
19881987
self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| {
@@ -2025,8 +2024,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20252024
}
20262025

20272026
(
2028-
b.span,
2029-
note_msg,
2027+
Spanned { node: note_msg, span: b.span },
20302028
help_msgs
20312029
.iter()
20322030
.enumerate()
@@ -2037,20 +2035,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20372035
.collect::<Vec<_>>(),
20382036
)
20392037
};
2040-
let (b1_span, b1_note_msg, b1_help_msgs) = could_refer_to(b1, misc1, "");
2041-
let (b2_span, b2_note_msg, b2_help_msgs) = could_refer_to(b2, misc2, " also");
2042-
2043-
AmbiguityErrorDiag {
2044-
msg: format!("`{ident}` is ambiguous"),
2045-
span: ident.span,
2046-
label_span: ident.span,
2047-
label_msg: "ambiguous name".to_string(),
2048-
note_msg: format!("ambiguous because of {}", kind.descr()),
2049-
b1_span,
2050-
b1_note_msg,
2038+
let (b1_note, b1_help_msgs) = could_refer_to(b1, misc1, "");
2039+
let (b2_note, b2_help_msgs) = could_refer_to(b2, misc2, " also");
2040+
2041+
errors::Ambiguity {
2042+
ident,
2043+
kind: kind.descr(),
2044+
b1_note,
20512045
b1_help_msgs,
2052-
b2_span,
2053-
b2_note_msg,
2046+
b2_note,
20542047
b2_help_msgs,
20552048
}
20562049
}

compiler/rustc_resolve/src/errors.rs

Lines changed: 44 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;
@@ -1415,3 +1416,44 @@ pub(crate) struct UnknownDiagnosticAttributeTypoSugg {
14151416
pub span: Span,
14161417
pub typo_name: Symbol,
14171418
}
1419+
1420+
// FIXME: Make this properly translatable.
1421+
pub(crate) struct Ambiguity {
1422+
pub ident: Ident,
1423+
pub kind: &'static str,
1424+
pub b1_note: Spanned<String>,
1425+
pub b1_help_msgs: Vec<String>,
1426+
pub b2_note: Spanned<String>,
1427+
pub b2_help_msgs: Vec<String>,
1428+
}
1429+
1430+
impl Ambiguity {
1431+
fn decorate<'a>(self, diag: &mut Diag<'a, impl EmissionGuarantee>) {
1432+
diag.primary_message(format!("`{}` is ambiguous", self.ident));
1433+
diag.code(E0759);
1434+
diag.span_label(self.ident.span, "ambiguous name");
1435+
diag.note(format!("ambiguous because of {}", self.kind));
1436+
diag.span_note(self.b1_note.span, self.b1_note.node);
1437+
for help_msg in self.b1_help_msgs {
1438+
diag.help(help_msg);
1439+
}
1440+
diag.span_note(self.b2_note.span, self.b2_note.node);
1441+
for help_msg in self.b2_help_msgs {
1442+
diag.help(help_msg);
1443+
}
1444+
}
1445+
}
1446+
1447+
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Ambiguity {
1448+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
1449+
let mut diag = Diag::new(dcx, level, "").with_span(self.ident.span);
1450+
self.decorate(&mut diag);
1451+
diag
1452+
}
1453+
}
1454+
1455+
impl<'a> LintDiagnostic<'a, ()> for Ambiguity {
1456+
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
1457+
self.decorate(diag);
1458+
}
1459+
}

tests/ui/imports/ambiguous-1.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | pub use self::handwritten::*;
99
|
1010
= note: `#[warn(ambiguous_glob_reexports)]` on by default
1111

12-
warning: `id` is ambiguous
12+
warning[E0759]: `id` is ambiguous
1313
--> $DIR/ambiguous-1.rs:29:5
1414
|
1515
LL | id();
@@ -38,8 +38,9 @@ LL | #![warn(ambiguous_glob_imports)]
3838

3939
warning: 2 warnings emitted
4040

41+
For more information about this error, try `rustc --explain E0759`.
4142
Future incompatibility report: Future breakage diagnostic:
42-
warning: `id` is ambiguous
43+
warning[E0759]: `id` is ambiguous
4344
--> $DIR/ambiguous-1.rs:29:5
4445
|
4546
LL | id();

tests/ui/imports/ambiguous-10.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `Token` is ambiguous
1+
error[E0759]: `Token` is ambiguous
22
--> $DIR/ambiguous-10.rs:15:9
33
|
44
LL | fn c(_: Token) {}
@@ -23,8 +23,9 @@ LL | use crate::b::*;
2323

2424
error: aborting due to 1 previous error
2525

26+
For more information about this error, try `rustc --explain E0759`.
2627
Future incompatibility report: Future breakage diagnostic:
27-
error: `Token` is ambiguous
28+
error[E0759]: `Token` is ambiguous
2829
--> $DIR/ambiguous-10.rs:15:9
2930
|
3031
LL | fn c(_: Token) {}

tests/ui/imports/ambiguous-12.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `b` is ambiguous
1+
error[E0759]: `b` is ambiguous
22
--> $DIR/ambiguous-12.rs:21:5
33
|
44
LL | b();
@@ -23,8 +23,9 @@ LL | use crate::public::*;
2323

2424
error: aborting due to 1 previous error
2525

26+
For more information about this error, try `rustc --explain E0759`.
2627
Future incompatibility report: Future breakage diagnostic:
27-
error: `b` is ambiguous
28+
error[E0759]: `b` is ambiguous
2829
--> $DIR/ambiguous-12.rs:21:5
2930
|
3031
LL | b();

tests/ui/imports/ambiguous-13.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `Rect` is ambiguous
1+
error[E0759]: `Rect` is ambiguous
22
--> $DIR/ambiguous-13.rs:18:9
33
|
44
LL | fn a(_: Rect) {}
@@ -23,8 +23,9 @@ LL | use crate::content::*;
2323

2424
error: aborting due to 1 previous error
2525

26+
For more information about this error, try `rustc --explain E0759`.
2627
Future incompatibility report: Future breakage diagnostic:
27-
error: `Rect` is ambiguous
28+
error[E0759]: `Rect` is ambiguous
2829
--> $DIR/ambiguous-13.rs:18:9
2930
|
3031
LL | fn a(_: Rect) {}

0 commit comments

Comments
 (0)