Skip to content

Commit 005d860

Browse files
authored
Rollup merge of #112979 - NotStirred:translatable_diag/resolve_imports, r=fee1-dead
Rewrite most diagnostics as translatable within resolve/imports
2 parents 5d9935a + b7d6032 commit 005d860

File tree

3 files changed

+168
-44
lines changed

3 files changed

+168
-44
lines changed

compiler/rustc_resolve/messages.ftl

+38
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

+81
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

+49-44
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,
@@ -774,9 +780,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
774780
}
775781
source_binding @ (Ok(..) | Err(Determined)) => {
776782
if source_binding.is_ok() {
777-
let msg = format!("`{}` is not directly importable", target);
778-
struct_span_err!(this.tcx.sess, import.span, E0253, "{}", &msg)
779-
.span_label(import.span, "cannot be imported directly")
783+
this.tcx
784+
.sess
785+
.create_err(IsNotDirectlyImportable { span: import.span, target })
780786
.emit();
781787
}
782788
let key = BindingKey::new(target, ns);
@@ -825,9 +831,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
825831
span_bug!(import.span, "inconsistent resolution for an import");
826832
}
827833
} else if self.privacy_errors.is_empty() {
828-
let msg = "cannot determine resolution for the import";
829-
let msg_note = "import resolution is stuck, try simplifying other imports";
830-
self.tcx.sess.struct_span_err(import.span, msg).note(msg_note).emit();
834+
self.tcx
835+
.sess
836+
.create_err(CannotDetermineImportResolution { span: import.span })
837+
.emit();
831838
}
832839

833840
module
@@ -938,8 +945,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
938945
&& let Some(max_vis) = max_vis.get()
939946
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
940947
{
941-
let msg = "glob import doesn't reexport anything because no candidate is public enough";
942-
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, id, import.span, msg);
948+
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, id, import.span, fluent::resolve_glob_import_doesnt_reexport);
943949
}
944950
return None;
945951
}
@@ -1011,10 +1017,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10111017
&& this.ambiguity_errors.is_empty()
10121018
&& this.privacy_errors.is_empty()
10131019
{
1014-
let msg = "cannot determine resolution for the import";
1015-
let msg_note =
1016-
"import resolution is stuck, try simplifying other imports";
1017-
this.tcx.sess.struct_span_err(import.span, msg).note(msg_note).emit();
1020+
this.tcx
1021+
.sess
1022+
.create_err(CannotDetermineImportResolution { span: import.span })
1023+
.emit();
10181024
}
10191025
}
10201026
Err(..) => {
@@ -1172,46 +1178,43 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11721178
msg,
11731179
);
11741180
} else {
1175-
let error_msg = if crate_private_reexport {
1176-
format!(
1177-
"`{}` is only public within the crate, and cannot be re-exported outside",
1178-
ident
1179-
)
1180-
} else {
1181-
format!("`{}` is private, and cannot be re-exported", ident)
1182-
};
1183-
11841181
if ns == TypeNS {
1185-
let label_msg = if crate_private_reexport {
1186-
format!("re-export of crate public `{}`", ident)
1182+
let mut err = if crate_private_reexport {
1183+
self.tcx.sess.create_err(CannotBeReexportedCratePublicNS {
1184+
span: import.span,
1185+
ident,
1186+
})
11871187
} else {
1188-
format!("re-export of private `{}`", ident)
1188+
self.tcx
1189+
.sess
1190+
.create_err(CannotBeReexportedPrivateNS { span: import.span, ident })
11891191
};
1190-
1191-
struct_span_err!(self.tcx.sess, import.span, E0365, "{}", error_msg)
1192-
.span_label(import.span, label_msg)
1193-
.note(format!("consider declaring type or module `{}` with `pub`", ident))
1194-
.emit();
1192+
err.emit();
11951193
} else {
1196-
let mut err =
1197-
struct_span_err!(self.tcx.sess, import.span, E0364, "{error_msg}");
1194+
let mut err = if crate_private_reexport {
1195+
self.tcx
1196+
.sess
1197+
.create_err(CannotBeReexportedCratePublic { span: import.span, ident })
1198+
} else {
1199+
self.tcx
1200+
.sess
1201+
.create_err(CannotBeReexportedPrivate { span: import.span, ident })
1202+
};
1203+
11981204
match binding.kind {
11991205
NameBindingKind::Res(Res::Def(DefKind::Macro(_), def_id))
12001206
// exclude decl_macro
12011207
if self.get_macro_by_def_id(def_id).macro_rules =>
12021208
{
1203-
err.span_help(
1204-
binding.span,
1205-
"consider adding a `#[macro_export]` to the macro in the imported module",
1206-
);
1209+
err.subdiagnostic(ConsiderAddingMacroExport {
1210+
span: binding.span,
1211+
});
12071212
}
12081213
_ => {
1209-
err.span_note(
1210-
import.span,
1211-
format!(
1212-
"consider marking `{ident}` as `pub` in the imported module"
1213-
),
1214-
);
1214+
err.subdiagnostic(ConsiderMarkingAsPub {
1215+
span: import.span,
1216+
ident,
1217+
});
12151218
}
12161219
}
12171220
err.emit();
@@ -1317,12 +1320,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13171320
let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() };
13181321

13191322
let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap() else {
1320-
self.tcx.sess.span_err(import.span, "cannot glob-import all possible crates");
1323+
self.tcx.sess.create_err(CannotGlobImportAllCrates {
1324+
span: import.span,
1325+
}).emit();
13211326
return;
13221327
};
13231328

13241329
if module.is_trait() {
1325-
self.tcx.sess.span_err(import.span, "items in traits are not importable");
1330+
self.tcx.sess.create_err(ItemsInTraitsAreNotImportable { span: import.span }).emit();
13261331
return;
13271332
} else if ptr::eq(module, import.parent_scope.module) {
13281333
return;

0 commit comments

Comments
 (0)