Skip to content

Commit

Permalink
Updates after variant compilation moved to HLIR
Browse files Browse the repository at this point in the history
  • Loading branch information
awelc committed Jun 24, 2024
1 parent 21da83a commit 489450f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use move_compiler::{
diagnostics as diag,
expansion::ast::{self as E, ModuleIdent},
naming::ast as N,
parser::ast as P,
parser::ast::{self as P, ConstantName},
shared::{files::MappedFiles, ide::MacroCallInfo, Identifier, Name},
typing::{
ast as T,
Expand Down Expand Up @@ -114,15 +114,12 @@ impl TypingAnalysisContext<'_> {
}

/// Add use of a const identifier
fn add_const_use_def(
&mut self,
module_ident: &E::ModuleIdent,
use_name: &Symbol,
use_pos: &Loc,
) {
fn add_const_use_def(&mut self, module_ident: &E::ModuleIdent, name: &ConstantName) {
if self.traverse_only {
return;
}
let use_pos = name.loc();
let use_name = name.value();
let mod_ident_str = expansion_mod_ident_to_map_key(&module_ident.value);
let Some(mod_defs) = self.mod_outer_defs.get(&mod_ident_str) else {
return;
Expand All @@ -145,11 +142,11 @@ impl TypingAnalysisContext<'_> {
);
}

let Some(name_start) = self.file_start_position_opt(use_pos) else {
let Some(name_start) = self.file_start_position_opt(&use_pos) else {
debug_assert!(false);
return;
};
if let Some(const_def) = mod_defs.constants.get(use_name) {
if let Some(const_def) = mod_defs.constants.get(&use_name) {
let const_info = self.def_info.get(&const_def.name_loc).unwrap();
let ident_type_def_loc = def_info_to_type_def_loc(self.mod_outer_defs, const_info);
self.use_defs.insert(
Expand All @@ -160,7 +157,7 @@ impl TypingAnalysisContext<'_> {
use_pos.file_hash(),
name_start.into(),
const_def.name_loc,
use_name,
&use_name,
ident_type_def_loc,
),
);
Expand Down Expand Up @@ -542,6 +539,73 @@ impl TypingAnalysisContext<'_> {
self.visit_exp(args);
}
}

fn process_match_patterm(&mut self, match_pat: &mut T::MatchPattern) {
use T::UnannotatedPat_ as UA;

self.visit_type(None, &mut match_pat.ty);
match &mut match_pat.pat.value {
UA::Variant(mident, name, vname, tyargs, fields)
| UA::BorrowVariant(_, mident, name, vname, tyargs, fields) => {
self.add_datatype_use_def(mident, name);
self.add_variant_use_def(mident, name, vname);
tyargs.iter_mut().for_each(|t| self.visit_type(None, t));
for (fpos, fname, (_, (_, pat))) in fields.iter_mut() {
self.add_variant_field_use_def(
&mident.value,
&name.value(),
&vname.value(),
fname,
&fpos,
);
self.process_match_patterm(pat);
}
}
UA::Struct(mident, name, tyargs, fields)
| UA::BorrowStruct(_, mident, name, tyargs, fields) => {
self.add_datatype_use_def(mident, name);
tyargs.iter_mut().for_each(|t| self.visit_type(None, t));
for (fpos, fname, (_, (_, pat))) in fields.iter_mut() {
self.add_struct_field_use_def(&mident.value, &name.value(), fname, &fpos);
self.process_match_patterm(pat);
}
}
UA::Constant(mod_ident, name) => self.add_const_use_def(mod_ident, name),
UA::Binder(mut_, var) => self.add_local_def(
&var.loc,
&var.value.name,
match_pat.ty.clone(),
false,
matches!(mut_, E::Mutability::Mut(_)),
),
UA::Or(pat1, pat2) => {
self.process_match_patterm(pat1);
self.process_match_patterm(pat2);
}
UA::At(var, pat) => {
self.add_local_def(
&var.loc,
&var.value.name,
match_pat.ty.clone(),
false,
false,
);
self.process_match_patterm(pat);
}
UA::Literal(_) | UA::Wildcard | UA::ErrorPat => (),
}
}

fn process_match_arm(&mut self, sp!(_, arm): &mut T::MatchArm) {
self.process_match_patterm(&mut arm.pattern);
arm.binders.iter_mut().for_each(|(var, ty)| {
self.add_local_def(&var.loc, &var.value.name, ty.clone(), false, false)
});
if let Some(exp) = &mut arm.guard {
self.visit_exp(exp);
}
self.visit_exp(&mut arm.rhs);
}
}

impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
Expand Down Expand Up @@ -882,7 +946,7 @@ impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
true
}
TE::Constant(mod_ident, name) => {
visitor.add_const_use_def(mod_ident, &name.value(), &name.loc());
visitor.add_const_use_def(mod_ident, &name);
true
}
TE::ModuleCall(mod_call) => {
Expand All @@ -897,20 +961,16 @@ impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
true
}
TE::Pack(mident, name, tyargs, fields) => {
// add use of the datatype name
visitor.add_datatype_use_def(mident, name);
for (fpos, fname, (_, (_, init_exp))) in fields.iter_mut() {
// add use of the field name
visitor.add_struct_field_use_def(
&mident.value,
&name.value(),
fname,
&fpos,
);
// add field initialization expression
visitor.visit_exp(init_exp);
}
// add type params
tyargs
.iter_mut()
.for_each(|t| visitor.visit_type(Some(exp_loc), t));
Expand All @@ -922,38 +982,37 @@ impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
true
}
TE::PackVariant(mident, name, vname, tyargs, fields) => {
// add use of the datatype name
visitor.add_datatype_use_def(mident, name);
for (fpos, fname, (_, (_, init_exp))) in fields.iter_mut() {
// add use of the field name
visitor.add_variant_field_use_def(
&mident.value,
&name.value(),
&vname.value(),
fname,
&fpos,
);
// add field initialization expression
visitor.visit_exp(init_exp);
}
// add type params
tyargs
.iter_mut()
.for_each(|t| visitor.visit_type(Some(exp_loc), t));
true
}
TE::VariantMatch(e, (mident, enum_name), v) => {
visitor.visit_exp(e);
visitor.add_datatype_use_def(mident, enum_name);
v.iter_mut().for_each(|(vname, e)| {
visitor.add_variant_use_def(mident, enum_name, vname);
visitor.visit_exp(e);
});
// visitor.visit_exp(e);
// visitor.add_datatype_use_def(mident, enum_name);
// v.iter_mut().for_each(|(vname, e)| {
// visitor.add_variant_use_def(mident, enum_name, vname);
// visitor.visit_exp(e);
// });

// These should not be available before match compilation.
debug_assert!(false);
true
}
TE::Match(_, _) => {
// These should be gone after match compilation.
debug_assert!(false);
TE::Match(exp, sp!(_, v)) => {
visitor.visit_exp(exp);
v.iter_mut().for_each(|arm| visitor.process_match_arm(arm));
true
}
TE::Unit { .. }
Expand Down
6 changes: 1 addition & 5 deletions external-crates/move/crates/move-analyzer/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Sym
for untyped_def_loc in mod_defs.untyped_defs() {
let start_position = symbols.files.start_position(untyped_def_loc);
if let Some(DefInfo::Local(n, t, _, _)) = symbols.def_info(untyped_def_loc) {
let position = Position {
line: start_position.line_offset() as u32,
character: start_position.column_offset() as u32 + n.len() as u32,
};
let colon_label = InlayHintLabelPart {
value: ": ".to_string(),
tooltip: None,
Expand All @@ -48,7 +44,7 @@ pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Sym
command: None,
};
let h = InlayHint {
position,
position: start_position.into(),
label: InlayHintLabel::LabelParts(vec![colon_label, type_label]),
kind: Some(InlayHintKind::TYPE),
text_edits: None,
Expand Down

0 comments on commit 489450f

Please sign in to comment.