From 837eec8dab32972a69f42891d55e71d49dcacea9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 21 Jul 2021 17:51:56 +0200 Subject: [PATCH] filter visiblities when resolving in extern crate --- crates/hir_def/src/nameres/collector.rs | 4 ++- crates/hir_def/src/nameres/tests.rs | 35 ++++++++++++++++---- crates/hir_def/src/nameres/tests/macros.rs | 4 +-- crates/hir_ty/src/tests/macros.rs | 2 +- crates/hir_ty/src/tests/method_resolution.rs | 4 +-- crates/hir_ty/src/tests/traits.rs | 4 +-- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 74362bb1a4c..1fd39d92941 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -747,7 +747,9 @@ fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialRe if let Some(krate) = res.krate { if krate != self.def_map.krate { - return PartialResolvedImport::Resolved(def); + return PartialResolvedImport::Resolved( + def.filter_visibility(|v| matches!(v, Visibility::Public)), + ); } } diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index 09f121c7ec2..ff34b4a9f27 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs @@ -296,7 +296,7 @@ fn edition_2015_imports() { use other_crate::FromLib; //- /lib.rs crate:other_crate edition:2018 -struct FromLib; +pub struct FromLib; "#, expect![[r#" crate @@ -371,7 +371,7 @@ fn extern_crate_rename() { use alloc_crate::Arc; //- /lib.rs crate:alloc -struct Arc; +pub struct Arc; "#, expect![[r#" crate @@ -397,7 +397,7 @@ fn extern_crate_rename_2015_edition() { use alloc_crate::Arc; //- /lib.rs crate:alloc -struct Arc; +pub struct Arc; "#, expect![[r#" crate @@ -476,13 +476,13 @@ fn no_std_prelude() { //- /core.rs crate:core pub mod prelude { - pud mod rust_2018 { + pub mod rust_2018 { pub struct Rust; } } //- /std.rs crate:std deps:core pub mod prelude { - pud mod rust_2018 { + pub mod rust_2018 { } } "#, @@ -505,7 +505,7 @@ fn edition_specific_preludes() { //- /std.rs crate:std pub mod prelude { - pud mod rust_2018 { + pub mod rust_2018 { pub struct Rust2018; } } @@ -522,7 +522,7 @@ pub mod prelude { //- /std.rs crate:std pub mod prelude { - pud mod rust_2021 { + pub mod rust_2021 { pub struct Rust2021; } } @@ -839,3 +839,24 @@ mod m { "#]], ); } + +#[test] +fn import_from_extern_crate_only_imports_public_items() { + check( + r#" +//- /lib.rs crate:lib deps:settings,macros +use macros::settings; +use settings::Settings; +//- /settings.rs crate:settings +pub struct Settings; +//- /macros.rs crate:macros +mod settings {} +pub const settings: () = (); + "#, + expect![[r#" + crate + Settings: t v + settings: v + "#]], + ) +} diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs index c2f68b7d543..730a4593d93 100644 --- a/crates/hir_def/src/nameres/tests/macros.rs +++ b/crates/hir_def/src/nameres/tests/macros.rs @@ -607,8 +607,8 @@ macro_rules! not_current2 { } } -struct Bar; -struct Baz; +pub struct Bar; +pub struct Baz; "#, expect![[r#" crate diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs index 2cf41e49e1b..ac5d749b092 100644 --- a/crates/hir_ty/src/tests/macros.rs +++ b/crates/hir_ty/src/tests/macros.rs @@ -27,7 +27,7 @@ fn test() { } //^ (i32, {unknown}, i32, {unknown}) //- /foo.rs crate:foo -struct S; +pub struct S; #[cfg(not(test))] impl S { diff --git a/crates/hir_ty/src/tests/method_resolution.rs b/crates/hir_ty/src/tests/method_resolution.rs index 3bf67281f2b..90eee77422e 100644 --- a/crates/hir_ty/src/tests/method_resolution.rs +++ b/crates/hir_ty/src/tests/method_resolution.rs @@ -256,8 +256,8 @@ fn test() { } //^ i128 //- /lib.rs crate:other_crate -mod foo { - struct S; +pub mod foo { + pub struct S; impl S { fn thing() -> i128 { 0 } } diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 9db30d9f98b..d5d0173ec11 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -209,8 +209,8 @@ pub mod rust_2018 { //- /alloc.rs crate:alloc deps:core #![no_std] -mod collections { - struct Vec {} +pub mod collections { + pub struct Vec {} impl Vec { pub fn new() -> Self { Vec {} } pub fn push(&mut self, t: T) { }