Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b7d6032

Browse files
committed
Add translatable diagnostic for import resolution strings
Add translatable diagnostic for cannot be reexported error also added for subdiagnostics Add translatable diagnostics for resolve_glob_import errors Add translatable diag for unable to determine import resolution Add translatable diag for is not directly importable
1 parent 54d6738 commit b7d6032

File tree

3 files changed

+168
-44
lines changed

3 files changed

+168
-44
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,26 @@ resolve_binding_shadows_something_unacceptable =
4444
resolve_binding_shadows_something_unacceptable_suggestion =
4545
try specify the pattern arguments
4646
47+
resolve_cannot_be_reexported_crate_public =
48+
`{$ident}` is only public within the crate, and cannot be re-exported outside
49+
50+
resolve_cannot_be_reexported_private =
51+
`{$ident}` is private, and cannot be re-exported
52+
4753
resolve_cannot_capture_dynamic_environment_in_fn_item =
4854
can't capture dynamic environment in a fn item
4955
.help = use the `|| {"{"} ... {"}"}` closure form instead
5056
57+
resolve_cannot_determine_import_resolution =
58+
cannot determine resolution for the import
59+
.note = import resolution is stuck, try simplifying other imports
60+
5161
resolve_cannot_find_ident_in_this_scope =
5262
cannot find {$expected} `{$ident}` in this scope
5363
64+
resolve_cannot_glob_import_possible_crates =
65+
cannot glob-import all possible crates
66+
5467
resolve_cannot_use_self_type_here =
5568
can't use `Self` here
5669
@@ -60,6 +73,15 @@ resolve_change_import_binding =
6073
resolve_consider_adding_a_derive =
6174
consider adding a derive
6275
76+
resolve_consider_adding_macro_export =
77+
consider adding a `#[macro_export]` to the macro in the imported module
78+
79+
resolve_consider_declaring_with_pub =
80+
consider declaring type or module `{$ident}` with `pub`
81+
82+
resolve_consider_marking_as_pub =
83+
consider marking `{$ident}` as `pub` in the imported module
84+
6385
resolve_const_not_member_of_trait =
6486
const `{$const_}` is not a member of trait `{$trait_}`
6587
.label = not a member of trait `{$trait_}`
@@ -98,6 +120,9 @@ resolve_generic_params_from_outer_function =
98120
.label = use of generic parameter from outer function
99121
.suggestion = try using a local generic parameter instead
100122
123+
resolve_glob_import_doesnt_reexport =
124+
glob import doesn't reexport anything because no candidate is public enough
125+
101126
resolve_help_try_using_local_generic_param =
102127
try using a local generic parameter instead
103128
@@ -122,6 +147,13 @@ resolve_invalid_asm_sym =
122147
.label = is a local variable
123148
.help = `sym` operands must refer to either a function or a static
124149
150+
resolve_is_not_directly_importable =
151+
`{$target}` is not directly importable
152+
.label = cannot be imported directly
153+
154+
resolve_items_in_traits_are_not_importable =
155+
items in traits are not importable
156+
125157
resolve_label_with_similar_name_reachable =
126158
a label with a similar name is reachable
127159
@@ -176,6 +208,12 @@ resolve_parent_module_reset_for_binding =
176208
resolve_proc_macro_same_crate = can't use a procedural macro from the same crate that defines it
177209
.help = you can define integration tests in a directory named `tests`
178210
211+
resolve_reexport_of_crate_public =
212+
re-export of crate public `{$ident}`
213+
214+
resolve_reexport_of_private =
215+
re-export of private `{$ident}`
216+
179217
resolve_relative_2018 =
180218
relative paths are not supported in visibilities in 2018 edition or later
181219
.suggestion = try

compiler/rustc_resolve/src/errors.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,84 @@ pub(crate) struct ConsiderAddingADerive {
646646
pub(crate) span: Span,
647647
pub(crate) suggestion: String,
648648
}
649+
650+
#[derive(Diagnostic)]
651+
#[diag(resolve_cannot_determine_import_resolution)]
652+
pub(crate) struct CannotDetermineImportResolution {
653+
#[primary_span]
654+
pub(crate) span: Span,
655+
}
656+
657+
#[derive(Diagnostic)]
658+
#[diag(resolve_cannot_be_reexported_private, code = "E0364")]
659+
pub(crate) struct CannotBeReexportedPrivate {
660+
#[primary_span]
661+
pub(crate) span: Span,
662+
pub(crate) ident: Ident,
663+
}
664+
665+
#[derive(Diagnostic)]
666+
#[diag(resolve_cannot_be_reexported_crate_public, code = "E0364")]
667+
pub(crate) struct CannotBeReexportedCratePublic {
668+
#[primary_span]
669+
pub(crate) span: Span,
670+
pub(crate) ident: Ident,
671+
}
672+
673+
#[derive(Diagnostic)]
674+
#[diag(resolve_cannot_be_reexported_private, code = "E0365")]
675+
#[note(resolve_consider_declaring_with_pub)]
676+
pub(crate) struct CannotBeReexportedPrivateNS {
677+
#[primary_span]
678+
#[label(resolve_reexport_of_private)]
679+
pub(crate) span: Span,
680+
pub(crate) ident: Ident,
681+
}
682+
683+
#[derive(Diagnostic)]
684+
#[diag(resolve_cannot_be_reexported_crate_public, code = "E0365")]
685+
#[note(resolve_consider_declaring_with_pub)]
686+
pub(crate) struct CannotBeReexportedCratePublicNS {
687+
#[primary_span]
688+
#[label(resolve_reexport_of_crate_public)]
689+
pub(crate) span: Span,
690+
pub(crate) ident: Ident,
691+
}
692+
693+
#[derive(Subdiagnostic)]
694+
#[help(resolve_consider_adding_macro_export)]
695+
pub(crate) struct ConsiderAddingMacroExport {
696+
#[primary_span]
697+
pub(crate) span: Span,
698+
}
699+
700+
#[derive(Subdiagnostic)]
701+
#[note(resolve_consider_marking_as_pub)]
702+
pub(crate) struct ConsiderMarkingAsPub {
703+
#[primary_span]
704+
pub(crate) span: Span,
705+
pub(crate) ident: Ident,
706+
}
707+
708+
#[derive(Diagnostic)]
709+
#[diag(resolve_cannot_glob_import_possible_crates)]
710+
pub(crate) struct CannotGlobImportAllCrates {
711+
#[primary_span]
712+
pub(crate) span: Span,
713+
}
714+
715+
#[derive(Diagnostic)]
716+
#[diag(resolve_items_in_traits_are_not_importable)]
717+
pub(crate) struct ItemsInTraitsAreNotImportable {
718+
#[primary_span]
719+
pub(crate) span: Span,
720+
}
721+
722+
#[derive(Diagnostic)]
723+
#[diag(resolve_is_not_directly_importable, code = "E0253")]
724+
pub(crate) struct IsNotDirectlyImportable {
725+
#[primary_span]
726+
#[label]
727+
pub(crate) span: Span,
728+
pub(crate) target: Ident,
729+
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
//! A bunch of methods and structures more or less related to resolving imports.
22
33
use crate::diagnostics::{import_candidates, DiagnosticMode, Suggestion};
4+
use crate::errors::{
5+
CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
6+
CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
7+
ConsiderAddingMacroExport, ConsiderMarkingAsPub, IsNotDirectlyImportable,
8+
ItemsInTraitsAreNotImportable,
9+
};
410
use crate::Determinacy::{self, *};
5-
use crate::Namespace::*;
11+
use crate::{fluent_generated as fluent, Namespace::*};
612
use crate::{module_to_string, names_to_string, ImportSuggestion};
713
use crate::{
814
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, ModuleKind, ResolutionError,
@@ -763,9 +769,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
763769
}
764770
source_binding @ (Ok(..) | Err(Determined)) => {
765771
if source_binding.is_ok() {
766-
let msg = format!("`{}` is not directly importable", target);
767-
struct_span_err!(this.tcx.sess, import.span, E0253, "{}", &msg)
768-
.span_label(import.span, "cannot be imported directly")
772+
this.tcx
773+
.sess
774+
.create_err(IsNotDirectlyImportable { span: import.span, target })
769775
.emit();
770776
}
771777
let key = BindingKey::new(target, ns);
@@ -814,9 +820,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
814820
span_bug!(import.span, "inconsistent resolution for an import");
815821
}
816822
} else if self.privacy_errors.is_empty() {
817-
let msg = "cannot determine resolution for the import";
818-
let msg_note = "import resolution is stuck, try simplifying other imports";
819-
self.tcx.sess.struct_span_err(import.span, msg).note(msg_note).emit();
823+
self.tcx
824+
.sess
825+
.create_err(CannotDetermineImportResolution { span: import.span })
826+
.emit();
820827
}
821828

822829
module
@@ -927,8 +934,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
927934
&& let Some(max_vis) = max_vis.get()
928935
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
929936
{
930-
let msg = "glob import doesn't reexport anything because no candidate is public enough";
931-
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, id, import.span, msg);
937+
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, id, import.span, fluent::resolve_glob_import_doesnt_reexport);
932938
}
933939
return None;
934940
}
@@ -1000,10 +1006,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10001006
&& this.ambiguity_errors.is_empty()
10011007
&& this.privacy_errors.is_empty()
10021008
{
1003-
let msg = "cannot determine resolution for the import";
1004-
let msg_note =
1005-
"import resolution is stuck, try simplifying other imports";
1006-
this.tcx.sess.struct_span_err(import.span, msg).note(msg_note).emit();
1009+
this.tcx
1010+
.sess
1011+
.create_err(CannotDetermineImportResolution { span: import.span })
1012+
.emit();
10071013
}
10081014
}
10091015
Err(..) => {
@@ -1161,46 +1167,43 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11611167
msg,
11621168
);
11631169
} else {
1164-
let error_msg = if crate_private_reexport {
1165-
format!(
1166-
"`{}` is only public within the crate, and cannot be re-exported outside",
1167-
ident
1168-
)
1169-
} else {
1170-
format!("`{}` is private, and cannot be re-exported", ident)
1171-
};
1172-
11731170
if ns == TypeNS {
1174-
let label_msg = if crate_private_reexport {
1175-
format!("re-export of crate public `{}`", ident)
1171+
let mut err = if crate_private_reexport {
1172+
self.tcx.sess.create_err(CannotBeReexportedCratePublicNS {
1173+
span: import.span,
1174+
ident,
1175+
})
11761176
} else {
1177-
format!("re-export of private `{}`", ident)
1177+
self.tcx
1178+
.sess
1179+
.create_err(CannotBeReexportedPrivateNS { span: import.span, ident })
11781180
};
1179-
1180-
struct_span_err!(self.tcx.sess, import.span, E0365, "{}", error_msg)
1181-
.span_label(import.span, label_msg)
1182-
.note(format!("consider declaring type or module `{}` with `pub`", ident))
1183-
.emit();
1181+
err.emit();
11841182
} else {
1185-
let mut err =
1186-
struct_span_err!(self.tcx.sess, import.span, E0364, "{error_msg}");
1183+
let mut err = if crate_private_reexport {
1184+
self.tcx
1185+
.sess
1186+
.create_err(CannotBeReexportedCratePublic { span: import.span, ident })
1187+
} else {
1188+
self.tcx
1189+
.sess
1190+
.create_err(CannotBeReexportedPrivate { span: import.span, ident })
1191+
};
1192+
11871193
match binding.kind {
11881194
NameBindingKind::Res(Res::Def(DefKind::Macro(_), def_id))
11891195
// exclude decl_macro
11901196
if self.get_macro_by_def_id(def_id).macro_rules =>
11911197
{
1192-
err.span_help(
1193-
binding.span,
1194-
"consider adding a `#[macro_export]` to the macro in the imported module",
1195-
);
1198+
err.subdiagnostic(ConsiderAddingMacroExport {
1199+
span: binding.span,
1200+
});
11961201
}
11971202
_ => {
1198-
err.span_note(
1199-
import.span,
1200-
format!(
1201-
"consider marking `{ident}` as `pub` in the imported module"
1202-
),
1203-
);
1203+
err.subdiagnostic(ConsiderMarkingAsPub {
1204+
span: import.span,
1205+
ident,
1206+
});
12041207
}
12051208
}
12061209
err.emit();
@@ -1306,12 +1309,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13061309
let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() };
13071310

13081311
let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap() else {
1309-
self.tcx.sess.span_err(import.span, "cannot glob-import all possible crates");
1312+
self.tcx.sess.create_err(CannotGlobImportAllCrates {
1313+
span: import.span,
1314+
}).emit();
13101315
return;
13111316
};
13121317

13131318
if module.is_trait() {
1314-
self.tcx.sess.span_err(import.span, "items in traits are not importable");
1319+
self.tcx.sess.create_err(ItemsInTraitsAreNotImportable { span: import.span }).emit();
13151320
return;
13161321
} else if ptr::eq(module, import.parent_scope.module) {
13171322
return;

0 commit comments

Comments
 (0)