Skip to content

Commit 50c971a

Browse files
committed
Add translatable diagnostic for invalid imports
1 parent 8fa9003 commit 50c971a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,6 @@ resolve_variable_bound_with_different_mode =
265265
266266
resolve_change_import_binding =
267267
you can use `as` to change the binding name of the import
268+
269+
resolve_imports_cannot_refer_to =
270+
imports cannot refer to {$what}

compiler/rustc_resolve/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,11 @@ pub(crate) struct ChangeImportBindingSuggestion {
605605
pub(crate) span: Span,
606606
pub(crate) suggestion: String,
607607
}
608+
609+
#[derive(Diagnostic)]
610+
#[diag(resolve_imports_cannot_refer_to)]
611+
pub(crate) struct ImportsCannotReferTo<'a> {
612+
#[primary_span]
613+
pub(crate) span: Span,
614+
pub(crate) what: &'a str,
615+
}

compiler/rustc_resolve/src/late.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! If you wonder why there's no `early.rs`, that's because it's split into three files -
77
//! `build_reduced_graph.rs`, `macros.rs` and `imports.rs`.
88
9+
use crate::errors::ImportsCannotReferTo;
910
use crate::BindingKey;
1011
use crate::{path_names_to_string, rustdoc, BindingError, Finalize, LexicalScopeBinding};
1112
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
@@ -2244,12 +2245,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
22442245
_ => &[TypeNS],
22452246
};
22462247
let report_error = |this: &Self, ns| {
2247-
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
22482248
if this.should_report_errs() {
2249-
this.r
2250-
.tcx
2251-
.sess
2252-
.span_err(ident.span, format!("imports cannot refer to {}", what));
2249+
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
2250+
2251+
let err = ImportsCannotReferTo { span: ident.span, what };
2252+
this.r.tcx.sess.create_err(err).emit();
22532253
}
22542254
};
22552255

0 commit comments

Comments
 (0)