|
1 | 1 | //! A bunch of methods and structures more or less related to resolving imports.
|
2 | 2 |
|
3 | 3 | 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 | +}; |
4 | 10 | use crate::Determinacy::{self, *};
|
5 |
| -use crate::Namespace::*; |
| 11 | +use crate::{fluent_generated as fluent, Namespace::*}; |
6 | 12 | use crate::{module_to_string, names_to_string, ImportSuggestion};
|
7 | 13 | use crate::{
|
8 | 14 | AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, ModuleKind, ResolutionError,
|
@@ -774,9 +780,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
774 | 780 | }
|
775 | 781 | source_binding @ (Ok(..) | Err(Determined)) => {
|
776 | 782 | 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 }) |
780 | 786 | .emit();
|
781 | 787 | }
|
782 | 788 | let key = BindingKey::new(target, ns);
|
@@ -825,9 +831,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
825 | 831 | span_bug!(import.span, "inconsistent resolution for an import");
|
826 | 832 | }
|
827 | 833 | } 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(); |
831 | 838 | }
|
832 | 839 |
|
833 | 840 | module
|
@@ -938,8 +945,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
938 | 945 | && let Some(max_vis) = max_vis.get()
|
939 | 946 | && !max_vis.is_at_least(import.expect_vis(), self.tcx)
|
940 | 947 | {
|
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); |
943 | 949 | }
|
944 | 950 | return None;
|
945 | 951 | }
|
@@ -1011,10 +1017,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
1011 | 1017 | && this.ambiguity_errors.is_empty()
|
1012 | 1018 | && this.privacy_errors.is_empty()
|
1013 | 1019 | {
|
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(); |
1018 | 1024 | }
|
1019 | 1025 | }
|
1020 | 1026 | Err(..) => {
|
@@ -1172,46 +1178,43 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
1172 | 1178 | msg,
|
1173 | 1179 | );
|
1174 | 1180 | } 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 |
| - |
1184 | 1181 | 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 | + }) |
1187 | 1187 | } else {
|
1188 |
| - format!("re-export of private `{}`", ident) |
| 1188 | + self.tcx |
| 1189 | + .sess |
| 1190 | + .create_err(CannotBeReexportedPrivateNS { span: import.span, ident }) |
1189 | 1191 | };
|
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(); |
1195 | 1193 | } 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 | + |
1198 | 1204 | match binding.kind {
|
1199 | 1205 | NameBindingKind::Res(Res::Def(DefKind::Macro(_), def_id))
|
1200 | 1206 | // exclude decl_macro
|
1201 | 1207 | if self.get_macro_by_def_id(def_id).macro_rules =>
|
1202 | 1208 | {
|
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 | + }); |
1207 | 1212 | }
|
1208 | 1213 | _ => {
|
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 | + }); |
1215 | 1218 | }
|
1216 | 1219 | }
|
1217 | 1220 | err.emit();
|
@@ -1317,12 +1320,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
1317 | 1320 | let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() };
|
1318 | 1321 |
|
1319 | 1322 | 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(); |
1321 | 1326 | return;
|
1322 | 1327 | };
|
1323 | 1328 |
|
1324 | 1329 | 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(); |
1326 | 1331 | return;
|
1327 | 1332 | } else if ptr::eq(module, import.parent_scope.module) {
|
1328 | 1333 | return;
|
|
0 commit comments