Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[move][move-ide] Revise how IDE information is recorded in the compiler #17864

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions external-crates/move/crates/move-analyzer/src/compiler_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use std::collections::{BTreeMap, BTreeSet};

use move_compiler::shared::ide as CI;
use move_ir_types::location::Loc;

#[derive(Default, Debug, Clone)]
pub struct CompilerInfo {
pub macro_info: BTreeMap<Loc, CI::MacroCallInfo>,
pub expanded_lambdas: BTreeSet<Loc>,
}

impl CompilerInfo {
pub fn new() -> CompilerInfo {
CompilerInfo::default()
}

pub fn from(info: impl IntoIterator<Item = (Loc, CI::IDEAnnotation)>) -> Self {
let mut result = Self::new();
result.add_info(info);
result
}

pub fn add_info(&mut self, info: impl IntoIterator<Item = (Loc, CI::IDEAnnotation)>) {
for (loc, entry) in info {
match entry {
CI::IDEAnnotation::MacroCallInfo(info) => {
// TODO: should we check this is not also an expanded lambda?
// TODO: what if we find two macro calls?
if let Some(_old) = self.macro_info.insert(loc, *info) {
eprintln!("Repeated macro info");
}
}
CI::IDEAnnotation::ExpandedLambda => {
self.expanded_lambdas.insert(loc);
}
}
}
}

pub fn get_macro_info(&mut self, loc: &Loc) -> Option<&CI::MacroCallInfo> {
self.macro_info.get(loc)
}

pub fn is_expanded_lambda(&mut self, loc: &Loc) -> bool {
self.expanded_lambdas.contains(loc)
}
}
1 change: 1 addition & 0 deletions external-crates/move/crates/move-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#[macro_use(sp)]
extern crate move_ir_types;

pub mod compiler_info;
pub mod completion;
pub mod context;
pub mod diagnostics;
Expand Down
89 changes: 48 additions & 41 deletions external-crates/move/crates/move-analyzer/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#![allow(clippy::non_canonical_partial_ord_impl)]

use crate::{
compiler_info::CompilerInfo,
cgswords marked this conversation as resolved.
Show resolved Hide resolved
context::Context,
diagnostics::{lsp_diagnostics, lsp_empty_diagnostics},
utils::get_loc,
Expand Down Expand Up @@ -98,10 +99,13 @@ use move_compiler::{
linters::LintLevel,
naming::ast::{StructDefinition, StructFields, TParam, Type, TypeName_, Type_, UseFuns},
parser::ast::{self as P, DatatypeName, FunctionName},
shared::{unique_map::UniqueMap, Identifier, Name, NamedAddressMap, NamedAddressMaps},
shared::{
ide::MacroCallInfo, unique_map::UniqueMap, Identifier, Name, NamedAddressMap,
NamedAddressMaps,
},
typing::ast::{
BuiltinFunction_, Exp, ExpListItem, Function, FunctionBody_, IDEInfo, LValue, LValueList,
LValue_, MacroCallInfo, ModuleDefinition, SequenceItem, SequenceItem_, UnannotatedExp_,
BuiltinFunction_, Exp, ExpListItem, Function, FunctionBody_, LValue, LValueList, LValue_,
ModuleDefinition, SequenceItem, SequenceItem_, UnannotatedExp_,
},
unit_test::filter_test_members::UNIT_TEST_POISON_FUN_NAME,
PASS_CFGIR, PASS_PARSER, PASS_TYPING,
Expand Down Expand Up @@ -390,6 +394,8 @@ pub struct TypingSymbolicator<'a> {
/// In some cases (e.g., when processing bodies of macros) we want to keep traversing
/// the AST but without recording the actual metadata (uses, definitions, types, etc.)
traverse_only: bool,
/// IDE Annotation Information from the Compiler
compiler_info: CompilerInfo,
}

/// Maps a line number to a list of use-def-s on a given line (use-def set is sorted by col_start)
Expand Down Expand Up @@ -717,7 +723,6 @@ fn ast_exp_to_ide_string(exp: &Exp) -> Option<String> {
.join(", "),
)
}
UE::IDEAnnotation(_, exp) => ast_exp_to_ide_string(exp),
UE::ExpList(list) => {
let items = list
.iter()
Expand Down Expand Up @@ -1194,6 +1199,7 @@ pub fn get_symbols(
BuildPlan::create(resolution_graph)?.set_compiler_vfs_root(overlay_fs_root.clone());
let mut parsed_ast = None;
let mut typed_ast = None;
let mut compiler_info = None;
let mut diagnostics = None;

let mut dependencies = build_plan.compute_dependencies();
Expand Down Expand Up @@ -1283,15 +1289,19 @@ pub fn get_symbols(
Ok(v) => v,
Err((_pass, diags)) => {
let failure = true;
// report_diagnostics(&files, diags);
diagnostics = Some((diags, failure));
eprintln!("typed AST compilation failed");
eprintln!("diagnostics: {:#?}", diagnostics);
return Ok((files, vec![]));
}
};
eprintln!("compiled to typed AST");
let (mut compiler, typed_program) = compiler.into_ast();
typed_ast = Some(typed_program.clone());

compiler_info = Some(CompilerInfo::from(
compiler.compilation_env().ide_information.clone(),
));
edition = Some(compiler.compilation_env().edition(Some(root_pkg_name)));

// compile to CFGIR for accurate diags
Expand Down Expand Up @@ -1399,7 +1409,6 @@ pub fn get_symbols(
&mut mod_to_alias_lengths,
);
}

let mut typing_symbolicator = TypingSymbolicator {
mod_outer_defs: &mod_outer_defs,
files: &files,
Expand All @@ -1410,6 +1419,7 @@ pub fn get_symbols(
use_defs: UseDefMap::new(),
alias_lengths: &BTreeMap::new(),
traverse_only: false,
compiler_info: compiler_info.unwrap(),
};

process_typed_modules(
Expand Down Expand Up @@ -2764,6 +2774,38 @@ impl<'a> TypingSymbolicator<'a> {

/// Get symbols for an expression
fn exp_symbols(&mut self, exp: &Exp, scope: &mut OrdMap<Symbol, LocalDef>) {
let expanded_lambda = self.compiler_info.is_expanded_lambda(&exp.exp.loc);
if let Some(macro_call_info) = self.compiler_info.get_macro_info(&exp.exp.loc) {
debug_assert!(!expanded_lambda, "Compiler info issue");
let MacroCallInfo {
module,
name,
method_name,
type_arguments,
by_value_args,
} = macro_call_info.clone();
self.mod_call_symbols(&module, name, method_name, &type_arguments, None, scope);
by_value_args
.iter()
.for_each(|a| self.seq_item_symbols(scope, a));
let old_traverse_mode = self.traverse_only;
// stop adding new use-defs etc.
self.traverse_only = true;
self.exp_symbols_inner(exp, scope);
self.traverse_only = old_traverse_mode;
} else if expanded_lambda {
let old_traverse_mode = self.traverse_only;
// start adding new use-defs etc. when processing a lambda argument
self.traverse_only = false;
self.exp_symbols_inner(exp, scope);
self.traverse_only = old_traverse_mode;
} else {
self.exp_symbols_inner(exp, scope);
}
}

/// Get symbols for an expression
fn exp_symbols_inner(&mut self, exp: &Exp, scope: &mut OrdMap<Symbol, LocalDef>) {
use UnannotatedExp_ as E;
match &exp.exp.value {
E::Move { from_user: _, var } => {
Expand Down Expand Up @@ -2840,41 +2882,6 @@ impl<'a> TypingSymbolicator<'a> {
self.traverse_only = old_traverse_mode;
}
}
E::IDEAnnotation(info, exp) => {
match info {
IDEInfo::MacroCallInfo(MacroCallInfo {
module,
name,
method_name,
type_arguments,
by_value_args,
}) => {
self.mod_call_symbols(
module,
*name,
*method_name,
type_arguments,
None,
scope,
);
by_value_args
.iter()
.for_each(|a| self.seq_item_symbols(scope, a));
let old_traverse_mode = self.traverse_only;
// stop adding new use-defs etc.
self.traverse_only = true;
self.exp_symbols(exp, scope);
self.traverse_only = old_traverse_mode;
}
IDEInfo::ExpandedLambda => {
let old_traverse_mode = self.traverse_only;
// start adding new use-defs etc. when processing a lambda argument
self.traverse_only = false;
self.exp_symbols(exp, scope);
self.traverse_only = old_traverse_mode;
}
}
}
E::Assign(lvalues, opt_types, e) => {
self.lvalue_list_symbols(false, lvalues, scope);
for opt_t in opt_types {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ fn tail(context: &mut Context, e: &T::Exp) -> Option<ControlFlow> {
}
}
E::Block((_, seq)) => tail_block(context, seq),
E::IDEAnnotation(_, e) => tail(context, e),

// -----------------------------------------------------------------------------------------
// statements
Expand Down Expand Up @@ -480,7 +479,6 @@ fn value(context: &mut Context, e: &T::Exp) -> Option<ControlFlow> {
}
}
E::Block((_, seq)) => value_block(context, seq),
E::IDEAnnotation(_, e) => value(context, e),

// -----------------------------------------------------------------------------------------
// calls and nested expressions
Expand Down Expand Up @@ -683,7 +681,6 @@ fn statement(context: &mut Context, e: &T::Exp) -> Option<ControlFlow> {
E::Block((_, seq)) => statement_block(
context, seq, /* stmt_pos */ true, /* skip_last */ false,
),
E::IDEAnnotation(_, e) => statement(context, e),
E::Return(rhs) => {
if let Some(rhs_control_flow) = value(context, rhs) {
context.report_value_error(rhs_control_flow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,6 @@ fn tail(
})
}
E::Block((_, seq)) => tail_block(context, block, expected_type, seq),
E::IDEAnnotation(_, e) => tail(context, block, expected_type, *e),

// -----------------------------------------------------------------------------------------
// statements that need to be hoisted out
Expand Down Expand Up @@ -1350,7 +1349,6 @@ fn value(
bound_exp
}
E::Block((_, seq)) => value_block(context, block, Some(&out_type), eloc, seq),
E::IDEAnnotation(_, e) => value(context, block, expected_type, *e),

// -----------------------------------------------------------------------------------------
// calls
Expand Down Expand Up @@ -1974,7 +1972,6 @@ fn statement(context: &mut Context, block: &mut Block, e: T::Exp) {
}
}
E::Block((_, seq)) => statement_block(context, block, seq),
E::IDEAnnotation(_, e) => statement(context, block, *e),
E::Return(rhs) => {
let expected_type = context.signature.as_ref().map(|s| s.return_type.clone());
let exp = value(context, block, expected_type.as_ref(), *rhs);
Expand Down
12 changes: 0 additions & 12 deletions external-crates/move/crates/move-compiler/src/naming/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,6 @@ pub enum MacroArgument {
Substituted(Loc),
}

#[derive(Debug, PartialEq, Clone)]
pub enum IDEInfo {
ExpandedLambda,
}

#[derive(Debug, PartialEq, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum Exp_ {
Expand Down Expand Up @@ -432,7 +427,6 @@ pub enum Exp_ {
While(BlockLabel, Box<Exp>, Box<Exp>),
Loop(BlockLabel, Box<Exp>),
Block(Block),
IDEAnnotation(IDEInfo, Box<Exp>),
Lambda(Lambda),

Assign(LValueList, Box<Exp>),
Expand Down Expand Up @@ -1737,12 +1731,6 @@ impl AstDebug for Exp_ {
e.ast_debug(w);
}
E::Block(seq) => seq.ast_debug(w),
E::IDEAnnotation(info, e) => match info {
IDEInfo::ExpandedLambda => {
w.write("ExpandedLambda:");
e.ast_debug(w);
}
},
E::Lambda(l) => l.ast_debug(w),
E::ExpList(es) => {
w.write("(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ fn exp(context: &mut Context, sp!(_, e_): &mut N::Exp) {
from_macro_argument: _,
seq,
}) => sequence(context, seq),
N::Exp_::IDEAnnotation(_, e) => exp(context, e),
N::Exp_::FieldMutate(ed, e) => {
exp_dotted(context, ed);
exp(context, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3785,7 +3785,6 @@ fn remove_unused_bindings_exp(
from_macro_argument: _,
seq,
}) => remove_unused_bindings_seq(context, used, seq),
N::Exp_::IDEAnnotation(_, e) => remove_unused_bindings_exp(context, used, e),
N::Exp_::Lambda(N::Lambda {
parameters: sp!(_, parameters),
return_label: _,
Expand Down
Loading
Loading