Skip to content

Commit

Permalink
[move][move-ide] Revise how IDE information is recorded in the compil…
Browse files Browse the repository at this point in the history
…er (#17864)

## Description 

This PR moves how we record IDE information to communicate it to the
Move Analyzer. It is now accrued on the `CompilationEnv` (modulo some
care in typing to handle type elaboration) and can be added through
methods there. The goal is to set up an extensible way to track IDE
information in the future, tying it to location information.

This change has also caused us to change how we compute spans for dotted
expression terms: previously, we would make the expression with the
call's span in order to report failures to autoborrow to point at the
call site (instead of just the subject term). This caused recurring
through location information in the analyzer to enter an infinite loop
in the macro case, as the subject term shared the location of the
overall macro call and recursion into the subject term on the macro
information looped forever. This diff introduces a solution to this
issue by more-carefully computing spans for subject terms so that they
remain pointing at the actual subject term, and plumbs the call location
through dotted expression handling to retain good error reporting for
the method case. It also introduces a very small change to handle
location reporting for `*`/`&`/`&mut ` usage for type errors.

## Test plan 

All tests still pass.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
cgswords authored May 29, 2024
1 parent 214719e commit a1ce332
Show file tree
Hide file tree
Showing 29 changed files with 450 additions and 519 deletions.
62 changes: 62 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,62 @@
// 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>,
pub autocomplete_info: BTreeMap<Loc, CI::AutocompleteInfo>,
}

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);
}
CI::IDEAnnotation::AutocompleteInfo(info) => {
// TODO: what if we find two autocomplete info sets? Intersection may be better
// than union, as it's likely in a lambda body.
if let Some(_old) = self.autocomplete_info.insert(loc, *info) {
eprintln!("Repeated autocomplete info");
}
}
}
}
}

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)
}

pub fn get_autocomplete_info(&mut self, loc: &Loc) -> Option<&CI::AutocompleteInfo> {
self.autocomplete_info.get(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 @@ -6,6 +6,7 @@
extern crate move_ir_types;

pub mod analyzer;
pub mod compiler_info;
pub mod completion;
pub mod context;
pub mod diagnostics;
Expand Down
91 changes: 47 additions & 44 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,
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 @@ -1285,13 +1291,16 @@ pub fn get_symbols(
let failure = true;
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 +1408,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 +1418,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 +2773,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 +2881,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 Expand Up @@ -2932,9 +2938,6 @@ impl<'a> TypingSymbolicator<'a> {
self.exp_symbols(exp, scope);
self.add_type_id_use_def(t);
}
E::AutocompleteDotAccess { base_exp, .. } => {
self.exp_symbols(base_exp, scope);
}
E::Unit { .. }
| E::Value(_)
| E::Continue(_)
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 @@ -528,8 +526,7 @@ fn value(context: &mut Context, e: &T::Exp) -> Option<ControlFlow> {
| E::UnaryExp(_, base_exp)
| E::Borrow(_, base_exp, _)
| E::Cast(base_exp, _)
| E::TempBorrow(_, base_exp)
| E::AutocompleteDotAccess { base_exp, .. } => value_report!(base_exp),
| E::TempBorrow(_, base_exp) => value_report!(base_exp),

E::BorrowLocal(_, _) => None,

Expand Down Expand Up @@ -683,7 +680,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 Expand Up @@ -742,7 +738,6 @@ fn statement(context: &mut Context, e: &T::Exp) -> Option<ControlFlow> {
| E::ErrorConstant { .. }
| E::Move { .. }
| E::Copy { .. }
| E::AutocompleteDotAccess { .. }
| E::UnresolvedError => value(context, e),

E::Value(_) | E::Unit { .. } => None,
Expand Down
33 changes: 1 addition & 32 deletions external-crates/move/crates/move-compiler/src/hlir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ use crate::{
parser::ast::{
Ability_, BinOp, BinOp_, ConstantName, DatatypeName, Field, FunctionName, VariantName,
},
shared::{
process_binops,
string_utils::{debug_print, format_oxford_list},
unique_map::UniqueMap,
*,
},
shared::{process_binops, string_utils::debug_print, unique_map::UniqueMap, *},
sui_mode::ID_FIELD_NAME,
typing::ast as T,
FullyCompiledProgram,
Expand Down Expand Up @@ -1044,7 +1039,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 @@ -1353,7 +1347,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 @@ -1692,28 +1685,6 @@ fn value(
assert!(context.env.has_errors());
make_exp(HE::UnresolvedError)
}
E::AutocompleteDotAccess {
base_exp: _,
methods,
fields,
} => {
if !(context.env.ide_mode()) {
context
.env
.add_diag(ice!((eloc, "Found autocomplete outside of IDE mode")));
};
let names = methods
.into_iter()
.map(|(m, f)| format!("{m}::{f}"))
.chain(fields.into_iter().map(|n| format!("{n}")))
.collect::<Vec<_>>();
let msg = format!(
"Autocompletes to: {}",
format_oxford_list!("or", "'{}'", names)
);
context.env.add_diag(diag!(IDE::Autocomplete, (eloc, msg)));
make_exp(HE::UnresolvedError)
}
};
maybe_freeze(context, block, expected_type.cloned(), preresult)
}
Expand Down Expand Up @@ -1977,7 +1948,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 Expand Up @@ -2055,7 +2025,6 @@ fn statement(context: &mut Context, block: &mut Block, e: T::Exp) {
| E::Move { .. }
| E::Copy { .. }
| E::UnresolvedError
| E::AutocompleteDotAccess { .. }
| E::NamedBlock(_, _)) => value_statement(context, block, make_exp(e_)),

E::Value(_) | E::Unit { .. } => (),
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
Loading

0 comments on commit a1ce332

Please sign in to comment.