Skip to content

Commit 51eb295

Browse files
authored
fix: LSP auto-import would import public item inside private module (#6366)
1 parent 83d29f2 commit 51eb295

9 files changed

Lines changed: 154 additions & 67 deletions

File tree

compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,12 @@ impl DefCollector {
303303
def_map.extern_prelude.insert(dep.as_name(), module_id);
304304

305305
let location = dep_def_map[dep_def_root].location;
306-
let attributes = ModuleAttributes { name: dep.as_name(), location, parent: None };
306+
let attributes = ModuleAttributes {
307+
name: dep.as_name(),
308+
location,
309+
parent: None,
310+
visibility: ItemVisibility::Public,
311+
};
307312
context.def_interner.add_module_attributes(module_id, attributes);
308313
}
309314

compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ fn push_child_module(
885885
name: mod_name.0.contents.clone(),
886886
location: mod_location,
887887
parent: Some(parent),
888+
visibility,
888889
},
889890
);
890891

compiler/noirc_frontend/src/node_interner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct ModuleAttributes {
5656
pub name: String,
5757
pub location: Location,
5858
pub parent: Option<LocalModuleId>,
59+
pub visibility: ItemVisibility,
5960
}
6061

6162
type StructAttributes = Vec<SecondaryAttribute>;

tooling/lsp/src/modules.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
use std::collections::BTreeMap;
2-
31
use noirc_frontend::{
4-
ast::ItemVisibility,
52
graph::{CrateId, Dependency},
6-
hir::def_map::{CrateDefMap, ModuleDefId, ModuleId},
3+
hir::def_map::{ModuleDefId, ModuleId},
74
node_interner::{NodeInterner, ReferenceId},
85
};
96

10-
use crate::visibility::is_visible;
11-
127
pub(crate) fn get_parent_module(
138
interner: &NodeInterner,
149
module_def_id: ModuleDefId,
@@ -33,18 +28,12 @@ pub(crate) fn module_def_id_to_reference_id(module_def_id: ModuleDefId) -> Refer
3328
/// - Otherwise, that item's parent module's path is returned
3429
pub(crate) fn relative_module_full_path(
3530
module_def_id: ModuleDefId,
36-
visibility: ItemVisibility,
3731
current_module_id: ModuleId,
3832
current_module_parent_id: Option<ModuleId>,
3933
interner: &NodeInterner,
40-
def_maps: &BTreeMap<CrateId, CrateDefMap>,
4134
) -> Option<String> {
4235
let full_path;
4336
if let ModuleDefId::ModuleId(module_id) = module_def_id {
44-
if !is_visible(module_id, current_module_id, visibility, def_maps) {
45-
return None;
46-
}
47-
4837
full_path = relative_module_id_path(
4938
module_id,
5039
&current_module_id,
@@ -56,10 +45,6 @@ pub(crate) fn relative_module_full_path(
5645
return None;
5746
};
5847

59-
if !is_visible(parent_module, current_module_id, visibility, def_maps) {
60-
return None;
61-
}
62-
6348
full_path = relative_module_id_path(
6449
parent_module,
6550
&current_module_id,

tooling/lsp/src/requests/code_action/import_or_qualify.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
use_segment_positions::{
1212
use_completion_item_additional_text_edits, UseCompletionItemAdditionTextEditsRequest,
1313
},
14+
visibility::module_def_id_is_visible,
1415
};
1516

1617
use super::CodeActionFinder;
@@ -41,6 +42,16 @@ impl<'a> CodeActionFinder<'a> {
4142
}
4243

4344
for (module_def_id, visibility, defining_module) in entries {
45+
if !module_def_id_is_visible(
46+
*module_def_id,
47+
self.module_id,
48+
*visibility,
49+
self.interner,
50+
self.def_maps,
51+
) {
52+
continue;
53+
}
54+
4455
let module_full_path = if let Some(defining_module) = defining_module {
4556
relative_module_id_path(
4657
*defining_module,
@@ -51,11 +62,9 @@ impl<'a> CodeActionFinder<'a> {
5162
} else {
5263
let Some(module_full_path) = relative_module_full_path(
5364
*module_def_id,
54-
*visibility,
5565
self.module_id,
5666
current_module_parent_id,
5767
self.interner,
58-
self.def_maps,
5968
) else {
6069
continue;
6170
};
@@ -132,7 +141,7 @@ mod tests {
132141

133142
let src = r#"
134143
mod foo {
135-
mod bar {
144+
pub mod bar {
136145
pub struct SomeTypeInBar {}
137146
}
138147
}
@@ -142,7 +151,7 @@ mod tests {
142151

143152
let expected = r#"
144153
mod foo {
145-
mod bar {
154+
pub mod bar {
146155
pub struct SomeTypeInBar {}
147156
}
148157
}
@@ -158,7 +167,7 @@ mod tests {
158167
let title = "Import foo::bar::SomeTypeInBar";
159168

160169
let src = r#"mod foo {
161-
mod bar {
170+
pub mod bar {
162171
pub struct SomeTypeInBar {}
163172
}
164173
}
@@ -168,7 +177,7 @@ fn foo(x: SomeType>|<InBar) {}"#;
168177
let expected = r#"use foo::bar::SomeTypeInBar;
169178
170179
mod foo {
171-
mod bar {
180+
pub mod bar {
172181
pub struct SomeTypeInBar {}
173182
}
174183
}
@@ -184,7 +193,7 @@ fn foo(x: SomeTypeInBar) {}"#;
184193

185194
let src = r#"
186195
mod foo {
187-
mod bar {
196+
pub mod bar {
188197
pub mod some_module_in_bar {}
189198
}
190199
}
@@ -196,7 +205,7 @@ fn foo(x: SomeTypeInBar) {}"#;
196205

197206
let expected = r#"
198207
mod foo {
199-
mod bar {
208+
pub mod bar {
200209
pub mod some_module_in_bar {}
201210
}
202211
}
@@ -214,7 +223,7 @@ fn foo(x: SomeTypeInBar) {}"#;
214223
let title = "Import foo::bar::some_module_in_bar";
215224

216225
let src = r#"mod foo {
217-
mod bar {
226+
pub mod bar {
218227
pub(crate) mod some_module_in_bar {}
219228
}
220229
}
@@ -226,7 +235,7 @@ fn main() {
226235
let expected = r#"use foo::bar::some_module_in_bar;
227236
228237
mod foo {
229-
mod bar {
238+
pub mod bar {
230239
pub(crate) mod some_module_in_bar {}
231240
}
232241
}
@@ -245,7 +254,7 @@ fn main() {
245254
let src = r#"use foo::bar::SomeOtherType;
246255
247256
mod foo {
248-
mod bar {
257+
pub mod bar {
249258
pub struct SomeTypeInBar {}
250259
}
251260
}
@@ -255,7 +264,7 @@ fn foo(x: SomeType>|<InBar) {}"#;
255264
let expected = r#"use foo::bar::{SomeOtherType, SomeTypeInBar};
256265
257266
mod foo {
258-
mod bar {
267+
pub mod bar {
259268
pub struct SomeTypeInBar {}
260269
}
261270
}

tooling/lsp/src/requests/completion.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use sort_text::underscore_sort_text;
3838

3939
use crate::{
4040
requests::to_lsp_location, trait_impl_method_stub_generator::TraitImplMethodStubGenerator,
41-
use_segment_positions::UseSegmentPositions, utils, visibility::is_visible, LspState,
41+
use_segment_positions::UseSegmentPositions, utils, visibility::item_in_module_is_visible,
42+
LspState,
4243
};
4344

4445
use super::process_request;
@@ -797,7 +798,12 @@ impl<'a> NodeFinder<'a> {
797798
if name_matches(name, prefix) {
798799
let per_ns = module_data.find_name(ident);
799800
if let Some((module_def_id, visibility, _)) = per_ns.types {
800-
if is_visible(module_id, self.module_id, visibility, self.def_maps) {
801+
if item_in_module_is_visible(
802+
module_id,
803+
self.module_id,
804+
visibility,
805+
self.def_maps,
806+
) {
801807
let completion_items = self.module_def_id_completion_items(
802808
module_def_id,
803809
name.clone(),
@@ -813,7 +819,12 @@ impl<'a> NodeFinder<'a> {
813819
}
814820

815821
if let Some((module_def_id, visibility, _)) = per_ns.values {
816-
if is_visible(module_id, self.module_id, visibility, self.def_maps) {
822+
if item_in_module_is_visible(
823+
module_id,
824+
self.module_id,
825+
visibility,
826+
self.def_maps,
827+
) {
817828
let completion_items = self.module_def_id_completion_items(
818829
module_def_id,
819830
name.clone(),

tooling/lsp/src/requests/completion/auto_import.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
use_segment_positions::{
66
use_completion_item_additional_text_edits, UseCompletionItemAdditionTextEditsRequest,
77
},
8+
visibility::module_def_id_is_visible,
89
};
910

1011
use super::{
@@ -33,6 +34,16 @@ impl<'a> NodeFinder<'a> {
3334
continue;
3435
}
3536

37+
if !module_def_id_is_visible(
38+
*module_def_id,
39+
self.module_id,
40+
*visibility,
41+
self.interner,
42+
self.def_maps,
43+
) {
44+
continue;
45+
}
46+
3647
let completion_items = self.module_def_id_completion_items(
3748
*module_def_id,
3849
name.clone(),
@@ -58,11 +69,9 @@ impl<'a> NodeFinder<'a> {
5869
} else {
5970
let Some(module_full_path) = relative_module_full_path(
6071
*module_def_id,
61-
*visibility,
6272
self.module_id,
6373
current_module_parent_id,
6474
self.interner,
65-
self.def_maps,
6675
) else {
6776
continue;
6877
};

0 commit comments

Comments
 (0)