diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index f38ce51d709c..7ff3f0966892 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1087,7 +1087,6 @@ fn resolve_ident_in_module_non_globs_unadjusted<'r>( orig_ident_span, binding, parent_scope, - module, finalize, shadowing, ); @@ -1150,7 +1149,6 @@ fn resolve_ident_in_module_globs_unadjusted<'r>( orig_ident_span, binding, parent_scope, - module, finalize, shadowing, ); @@ -1260,7 +1258,6 @@ fn finalize_module_binding( orig_ident_span: Span, binding: Option>, parent_scope: &ParentScope<'ra>, - module: Module<'ra>, finalize: Finalize, shadowing: Shadowing, ) -> Result, ControlFlow> { @@ -1295,17 +1292,6 @@ fn finalize_module_binding( self.macro_expanded_macro_export_errors.insert((path_span, binding.span)); } - // If we encounter a re-export for a type with private fields, it will not be able to - // be constructed through this re-export. We track that case here to expand later - // privacy errors with appropriate information. - if let Res::Def(_, def_id) = binding.res() { - if let Some(ctor) = self.struct_ctor(def_id) - && ctor.has_private_fields(module, self) - { - self.inaccessible_ctor_reexport.insert(path_span, binding.span); - } - } - self.record_use(ident, binding, used); return Ok(binding); } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 634893354c9e..cbcf1a182c7e 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2368,16 +2368,28 @@ fn smart_resolve_context_dependent_help( return true; }; + // A type is re-exported and has an inaccessible constructor because it has fields + // that are inaccessible from the reexport's scope, extend the diagnostic. let is_accessible = self.r.is_accessible_from(ctor.vis, self.parent_scope.module); - if let Some(use_span) = self.r.inaccessible_ctor_reexport.get(&span) - && is_accessible + if is_accessible + && let mod_path = &path[..path.len() - 1] + && let PathResult::Module(ModuleOrUniformRoot::Module(import_mod)) = + self.resolve_path(mod_path, Some(TypeNS), None, PathSource::Module) + && ctor.has_private_fields(import_mod, self.r) + && let Ok(import_decl) = self.r.cm().maybe_resolve_ident_in_module( + ModuleOrUniformRoot::Module(import_mod), + path.last().unwrap().ident, + TypeNS, + &self.parent_scope, + None, + ) { err.span_note( - *use_span, + import_decl.span, "the type is accessed through this re-export, but the type's constructor \ is not visible in this import's scope due to private fields", ); - if is_accessible && !ctor.has_private_fields(self.parent_scope.module, self.r) { + if !ctor.has_private_fields(self.parent_scope.module, self.r) { err.span_suggestion_verbose( span, "the type can be constructed directly, because its fields are \ diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5247777e2287..35ef00f94503 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1285,11 +1285,6 @@ pub struct Resolver<'ra, 'tcx> { /// Crate-local macro expanded `macro_export` referred to by a module-relative path. macro_expanded_macro_export_errors: BTreeSet<(Span, Span)> = BTreeSet::new(), - /// When a type is re-exported that has an inaccessible constructor because it has fields that - /// are inaccessible from the import's scope, we mark that as the type won't be able to be built - /// through the re-export. We use this information to extend the existing diagnostic. - inaccessible_ctor_reexport: FxHashMap = default::fx_hash_map(), - arenas: &'ra ResolverArenas<'ra>, dummy_decl: Decl<'ra>, builtin_type_decls: FxHashMap>,