Skip to content

Commit 7e1ec7e

Browse files
alanzfacebook-github-bot
authored andcommitted
Add index from FunctionId to FunctionDef in DefMap
Summary: Code being developed may accidentally have distinct functions with the same `NameArity`. Keep an index to the `FunctionDef` via the form `FunctionId`. This will allow us to later lower single-clause `ast::FunDecl`s by retrieving the source via this. Reviewed By: TD5 Differential Revision: D50371713 fbshipit-source-id: bdb09a04001544fa39fed4299e07e4ca7b5f34bd
1 parent 052960d commit 7e1ec7e

File tree

1 file changed

+78
-13
lines changed

1 file changed

+78
-13
lines changed

crates/hir/src/def_map.rs

+78-13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::DefineDef;
4040
use crate::File;
4141
use crate::FormIdx;
4242
use crate::FunctionDef;
43+
use crate::FunctionId;
4344
use crate::InFile;
4445
use crate::MacroName;
4546
use crate::Name;
@@ -66,6 +67,7 @@ pub struct DefMap {
6667
macros: FxHashMap<MacroName, DefineDef>,
6768
export_all: bool,
6869
pub parse_transform: bool,
70+
function_by_function_id: FxHashMap<FunctionId, FunctionDef>,
6971
}
7072

7173
#[derive(Debug, Clone, PartialEq, Eq, Default)]
@@ -122,18 +124,20 @@ impl DefMap {
122124
match form {
123125
FormIdx::Function(idx) => {
124126
let function = form_list[idx].clone();
125-
def_map.functions.insert(
126-
function.name.clone(),
127-
FunctionDef {
128-
file,
129-
exported: false,
130-
deprecated: false,
131-
deprecated_desc: None,
132-
module: module.clone(),
133-
function,
134-
function_id: idx,
135-
},
136-
);
127+
let fun_name = &function.name.clone();
128+
let function_def = FunctionDef {
129+
file,
130+
exported: false,
131+
deprecated: false,
132+
deprecated_desc: None,
133+
module: module.clone(),
134+
function,
135+
function_id: idx,
136+
};
137+
def_map
138+
.functions
139+
.insert(fun_name.clone(), function_def.clone());
140+
def_map.function_by_function_id.insert(idx, function_def);
137141
}
138142
FormIdx::Export(idx) => {
139143
for export_id in form_list[idx].entries.clone() {
@@ -544,6 +548,7 @@ impl DefMap {
544548
export_all: _,
545549
parse_transform: _,
546550
optional_callbacks,
551+
function_by_function_id: function_by_form_id,
547552
} = self;
548553

549554
included.shrink_to_fit();
@@ -558,6 +563,7 @@ impl DefMap {
558563
callbacks.shrink_to_fit();
559564
macros.shrink_to_fit();
560565
deprecated.shrink_to_fit();
566+
function_by_form_id.shrink_to_fit();
561567
}
562568
}
563569

@@ -582,7 +588,35 @@ mod tests {
582588
format!(
583589
"fun {} exported: {}",
584590
def.function.name,
585-
def.exported && def_map.exported_functions.contains(&def.function.name)
591+
def.exported && def_map.exported_functions.contains(&def.function.name),
592+
)
593+
})
594+
.chain(def_map.types.values().map(|def| match &def.type_alias {
595+
TypeAlias::Regular { name, .. } => {
596+
format!("-type {} exported: {}", name, def.exported)
597+
}
598+
TypeAlias::Opaque { name, .. } => {
599+
format!("-opaque {} exported: {}", name, def.exported)
600+
}
601+
}))
602+
.collect::<Vec<_>>()
603+
.join("\n");
604+
resolved.push('\n');
605+
expect.assert_eq(&resolved);
606+
}
607+
608+
fn check_functions_by_id(fixture: &str, expect: Expect) {
609+
let (db, files) = TestDB::with_many_files(fixture);
610+
let file_id = files[0];
611+
let def_map = db.def_map(file_id);
612+
let mut resolved = def_map
613+
.function_by_function_id
614+
.values()
615+
.map(|def| {
616+
format!(
617+
"fun {} exported: {}",
618+
def.function.name,
619+
def.exported && def_map.exported_functions.contains(&def.function.name),
586620
)
587621
})
588622
.chain(def_map.types.values().map(|def| match &def.type_alias {
@@ -785,4 +819,35 @@ bar() -> ok.
785819
"#]],
786820
)
787821
}
822+
823+
#[test]
824+
fn multiple_mfas() {
825+
check_functions(
826+
r#"
827+
foo(A,B,C) -> {A,B,C}.
828+
bar() -> ok.
829+
foo(X,Y, Z) -> ok.
830+
"#,
831+
expect![[r#"
832+
fun foo/3 exported: false
833+
fun bar/0 exported: false
834+
"#]],
835+
)
836+
}
837+
838+
#[test]
839+
fn multiple_mfas_by_id() {
840+
check_functions_by_id(
841+
r#"
842+
foo(A,B,C) -> {A,B,C}.
843+
bar() -> ok.
844+
foo(X,Y, Z) -> ok.
845+
"#,
846+
expect![[r#"
847+
fun foo/3 exported: false
848+
fun bar/0 exported: false
849+
fun foo/3 exported: false
850+
"#]],
851+
)
852+
}
788853
}

0 commit comments

Comments
 (0)