Skip to content

Commit 756e917

Browse files
Merge #4305
4305: Favor types for record type struct in name resolution r=matklad a=edwin0cheng Fixed #4235 Co-authored-by: Edwin Cheng <[email protected]>
2 parents f68e099 + 07de8ea commit 756e917

File tree

7 files changed

+90
-28
lines changed

7 files changed

+90
-28
lines changed

crates/ra_hir_def/src/body/lower.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,16 @@ impl ExprCollector<'_> {
573573
self.body.item_scope.define_def(def);
574574
if let Some(name) = name {
575575
let vis = crate::visibility::Visibility::Public; // FIXME determine correctly
576-
self.body
577-
.item_scope
578-
.push_res(name.as_name(), crate::per_ns::PerNs::from_def(def, vis));
576+
let has_constructor = match def {
577+
ModuleDefId::AdtId(AdtId::StructId(s)) => {
578+
self.db.struct_data(s).variant_data.kind() != StructKind::Record
579+
}
580+
_ => true,
581+
};
582+
self.body.item_scope.push_res(
583+
name.as_name(),
584+
crate::per_ns::PerNs::from_def(def, vis, has_constructor),
585+
);
579586
}
580587
}
581588
}

crates/ra_hir_def/src/item_scope.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,20 @@ impl ItemScope {
151151
}
152152

153153
impl PerNs {
154-
pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs {
154+
pub(crate) fn from_def(def: ModuleDefId, v: Visibility, has_constructor: bool) -> PerNs {
155155
match def {
156156
ModuleDefId::ModuleId(_) => PerNs::types(def, v),
157157
ModuleDefId::FunctionId(_) => PerNs::values(def, v),
158158
ModuleDefId::AdtId(adt) => match adt {
159-
AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def, v),
159+
AdtId::UnionId(_) => PerNs::types(def, v),
160160
AdtId::EnumId(_) => PerNs::types(def, v),
161+
AdtId::StructId(_) => {
162+
if has_constructor {
163+
PerNs::both(def, def, v)
164+
} else {
165+
PerNs::types(def, v)
166+
}
167+
}
161168
},
162169
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v),
163170
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v),

crates/ra_hir_def/src/nameres/collector.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl ModCollector<'_, '_> {
830830
let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
831831
let def: ModuleDefId = module.into();
832832
self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
833-
self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis);
833+
self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis, false))], vis);
834834
res
835835
}
836836

@@ -844,14 +844,17 @@ impl ModCollector<'_, '_> {
844844
let name = def.name.clone();
845845
let container = ContainerId::ModuleId(module);
846846
let vis = &def.visibility;
847+
let mut has_constructor = false;
848+
847849
let def: ModuleDefId = match def.kind {
848850
raw::DefKind::Function(ast_id) => FunctionLoc {
849851
container: container.into(),
850852
ast_id: AstId::new(self.file_id, ast_id),
851853
}
852854
.intern(self.def_collector.db)
853855
.into(),
854-
raw::DefKind::Struct(ast_id) => {
856+
raw::DefKind::Struct(ast_id, mode) => {
857+
has_constructor = mode != raw::StructDefKind::Record;
855858
StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
856859
.intern(self.def_collector.db)
857860
.into()
@@ -894,7 +897,11 @@ impl ModCollector<'_, '_> {
894897
.def_map
895898
.resolve_visibility(self.def_collector.db, self.module_id, vis)
896899
.unwrap_or(Visibility::Public);
897-
self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis)
900+
self.def_collector.update(
901+
self.module_id,
902+
&[(name, PerNs::from_def(def, vis, has_constructor))],
903+
vis,
904+
)
898905
}
899906

900907
fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {

crates/ra_hir_def/src/nameres/raw.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,17 @@ pub(super) struct DefData {
155155
pub(super) visibility: RawVisibility,
156156
}
157157

158+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
159+
pub(super) enum StructDefKind {
160+
Record,
161+
Tuple,
162+
Unit,
163+
}
164+
158165
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
159166
pub(super) enum DefKind {
160167
Function(FileAstId<ast::FnDef>),
161-
Struct(FileAstId<ast::StructDef>),
168+
Struct(FileAstId<ast::StructDef>, StructDefKind),
162169
Union(FileAstId<ast::UnionDef>),
163170
Enum(FileAstId<ast::EnumDef>),
164171
Const(FileAstId<ast::ConstDef>),
@@ -171,7 +178,7 @@ impl DefKind {
171178
pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> {
172179
match self {
173180
DefKind::Function(it) => it.upcast(),
174-
DefKind::Struct(it) => it.upcast(),
181+
DefKind::Struct(it, _) => it.upcast(),
175182
DefKind::Union(it) => it.upcast(),
176183
DefKind::Enum(it) => it.upcast(),
177184
DefKind::Const(it) => it.upcast(),
@@ -236,9 +243,14 @@ impl RawItemsCollector {
236243
return;
237244
}
238245
ast::ModuleItem::StructDef(it) => {
246+
let kind = match it.kind() {
247+
ast::StructKind::Record(_) => StructDefKind::Record,
248+
ast::StructKind::Tuple(_) => StructDefKind::Tuple,
249+
ast::StructKind::Unit => StructDefKind::Unit,
250+
};
239251
let id = self.source_ast_id_map.ast_id(&it);
240252
let name = it.name();
241-
(DefKind::Struct(id), name)
253+
(DefKind::Struct(id, kind), name)
242254
}
243255
ast::ModuleItem::UnionDef(it) => {
244256
let id = self.source_ast_id_map.ast_id(&it);

crates/ra_hir_def/src/nameres/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn crate_def_map_smoke_test() {
6767
⋮Baz: t v
6868
⋮E: t
6969
⋮EXT: v
70-
⋮U: t v
70+
⋮U: t
7171
⋮ext: v
7272
"###)
7373
}

crates/ra_hir_def/src/nameres/tests/macros.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ fn macro_rules_are_globally_visible() {
1919
);
2020
assert_snapshot!(map, @r###"
2121
⋮crate
22-
⋮Foo: t v
22+
⋮Foo: t
2323
⋮nested: t
2424
2525
⋮crate::nested
26-
⋮Bar: t v
27-
⋮Baz: t v
26+
⋮Bar: t
27+
⋮Baz: t
2828
"###);
2929
}
3030

@@ -91,13 +91,13 @@ fn macro_rules_from_other_crates_are_visible() {
9191
);
9292
assert_snapshot!(map, @r###"
9393
⋮crate
94-
⋮Bar: t v
95-
⋮Foo: t v
94+
⋮Bar: t
95+
⋮Foo: t
9696
⋮bar: t
9797
9898
⋮crate::bar
99-
⋮Bar: t v
100-
⋮Foo: t v
99+
⋮Bar: t
100+
⋮Foo: t
101101
⋮bar: t
102102
"###);
103103
}
@@ -124,13 +124,13 @@ fn macro_rules_export_with_local_inner_macros_are_visible() {
124124
);
125125
assert_snapshot!(map, @r###"
126126
⋮crate
127-
⋮Bar: t v
128-
⋮Foo: t v
127+
⋮Bar: t
128+
⋮Foo: t
129129
⋮bar: t
130130
131131
⋮crate::bar
132-
⋮Bar: t v
133-
⋮Foo: t v
132+
⋮Bar: t
133+
⋮Foo: t
134134
⋮bar: t
135135
"###);
136136
}
@@ -161,13 +161,13 @@ fn local_inner_macros_makes_local_macros_usable() {
161161
);
162162
assert_snapshot!(map, @r###"
163163
⋮crate
164-
⋮Bar: t v
165-
⋮Foo: t v
164+
⋮Bar: t
165+
⋮Foo: t
166166
⋮bar: t
167167
168168
⋮crate::bar
169-
⋮Bar: t v
170-
⋮Foo: t v
169+
⋮Bar: t
170+
⋮Foo: t
171171
⋮bar: t
172172
"###);
173173
}
@@ -204,7 +204,7 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
204204
);
205205
assert_snapshot!(map, @r###"
206206
⋮crate
207-
⋮Foo: t v
207+
⋮Foo: t
208208
⋮bar: m
209209
⋮foo: m
210210
"###);

crates/ra_hir_ty/src/tests/regression.rs

+29
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,35 @@ fn foo(b: Bar) {
534534
);
535535
}
536536

537+
#[test]
538+
fn issue_4235_name_conflicts() {
539+
assert_snapshot!(
540+
infer(r#"
541+
struct FOO {}
542+
static FOO:FOO = FOO {};
543+
544+
impl FOO {
545+
fn foo(&self) {}
546+
}
547+
548+
fn main() {
549+
let a = &FOO;
550+
a.foo();
551+
}
552+
"#), @r###"
553+
32..38 'FOO {}': FOO
554+
64..68 'self': &FOO
555+
70..72 '{}': ()
556+
86..120 '{ ...o(); }': ()
557+
96..97 'a': &FOO
558+
100..104 '&FOO': &FOO
559+
101..104 'FOO': FOO
560+
110..111 'a': &FOO
561+
110..117 'a.foo()': ()
562+
"###
563+
);
564+
}
565+
537566
#[test]
538567
fn issue_4053_diesel_where_clauses() {
539568
assert_snapshot!(

0 commit comments

Comments
 (0)