|
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, |
@@ -763,9 +769,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { |
763 | 769 | } |
764 | 770 | source_binding @ (Ok(..) | Err(Determined)) => { |
765 | 771 | 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 }) |
769 | 775 | .emit(); |
770 | 776 | } |
771 | 777 | let key = BindingKey::new(target, ns); |
@@ -814,9 +820,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { |
814 | 820 | span_bug!(import.span, "inconsistent resolution for an import"); |
815 | 821 | } |
816 | 822 | } 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(); |
820 | 827 | } |
821 | 828 |
|
822 | 829 | module |
@@ -927,8 +934,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { |
927 | 934 | && let Some(max_vis) = max_vis.get() |
928 | 935 | && !max_vis.is_at_least(import.expect_vis(), self.tcx) |
929 | 936 | { |
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); |
932 | 938 | } |
933 | 939 | return None; |
934 | 940 | } |
@@ -1000,10 +1006,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { |
1000 | 1006 | && this.ambiguity_errors.is_empty() |
1001 | 1007 | && this.privacy_errors.is_empty() |
1002 | 1008 | { |
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(); |
1007 | 1013 | } |
1008 | 1014 | } |
1009 | 1015 | Err(..) => { |
@@ -1161,46 +1167,43 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { |
1161 | 1167 | msg, |
1162 | 1168 | ); |
1163 | 1169 | } 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 | | - |
1173 | 1170 | 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 | + }) |
1176 | 1176 | } else { |
1177 | | - format!("re-export of private `{}`", ident) |
| 1177 | + self.tcx |
| 1178 | + .sess |
| 1179 | + .create_err(CannotBeReexportedPrivateNS { span: import.span, ident }) |
1178 | 1180 | }; |
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(); |
1184 | 1182 | } 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 | + |
1187 | 1193 | match binding.kind { |
1188 | 1194 | NameBindingKind::Res(Res::Def(DefKind::Macro(_), def_id)) |
1189 | 1195 | // exclude decl_macro |
1190 | 1196 | if self.get_macro_by_def_id(def_id).macro_rules => |
1191 | 1197 | { |
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 | + }); |
1196 | 1201 | } |
1197 | 1202 | _ => { |
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 | + }); |
1204 | 1207 | } |
1205 | 1208 | } |
1206 | 1209 | err.emit(); |
@@ -1306,12 +1309,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { |
1306 | 1309 | let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() }; |
1307 | 1310 |
|
1308 | 1311 | 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(); |
1310 | 1315 | return; |
1311 | 1316 | }; |
1312 | 1317 |
|
1313 | 1318 | 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(); |
1315 | 1320 | return; |
1316 | 1321 | } else if ptr::eq(module, import.parent_scope.module) { |
1317 | 1322 | return; |
|
0 commit comments