refactor(parser): use var_id for most constants in ResolvedImportPattern (#14920)

# Description

This PR replaces most of the constants in `ResolvedImportPattern` from
values to VarIds, this has benefits of:
1. less duplicated variables in state
2. precise span of variable, for example when calling `goto def` on a
const imported by the `use` command, this allows it to find the original
definition, instead of where the `use` command is.

Note that the logic is different here for nested submodules, not all
values are flattened and propagated to the outmost record variable, but
I didn't find any differences in real world usage.

I noticed that it was changed from `VarId` to `Value` in #10049.
Maybe @kubouch can find some edge cases where this PR fails to work as
expected.

In my view, the record constants for `ResolvedImportPattern` should even
reduced to single entry, if not able to get rid of.

# User-Facing Changes

# Tests + Formatting

# After Submitting
This commit is contained in:
zc he
2025-01-26 21:43:34 +08:00
committed by GitHub
parent f88ed6ecd5
commit f46f8b286b
3 changed files with 58 additions and 24 deletions

View File

@ -2468,7 +2468,11 @@ pub fn parse_use(
let mut constants = vec![];
for (name, const_val) in definitions.constants {
for (name, const_vid) in definitions.constants {
constants.push((name, const_vid));
}
for (name, const_val) in definitions.constant_values {
let const_var_id =
working_set.add_variable(name.clone(), name_span, const_val.get_type(), false);
working_set.set_variable_const_val(const_var_id, const_val);
@ -2967,7 +2971,10 @@ pub fn parse_overlay_use(working_set: &mut StateWorkingSet, call: Box<Call>) ->
)
}
} else {
(ResolvedImportPattern::new(vec![], vec![], vec![]), vec![])
(
ResolvedImportPattern::new(vec![], vec![], vec![], vec![]),
vec![],
)
};
if errors.is_empty() {