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-compiler] Added dot chain parsing resilience #17106

Merged
merged 8 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 115 additions & 1 deletion external-crates/move/crates/move-analyzer/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2691,7 +2691,9 @@ impl<'a> TypingSymbolicator<'a> {
self.exp_symbols(exp, scope);
self.add_type_id_use_def(t);
}

E::InvalidAccess(e) => {
self.exp_symbols(e, scope);
}
_ => (),
}
}
Expand Down Expand Up @@ -6791,3 +6793,115 @@ fn partial_function_test() {
None,
);
}

#[test]
/// Tests if partial dot chains are symbolicated correctly.
fn partial_dot_test() {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

path.push("tests/partial-dot");

let ide_files_layer: VfsPath = MemoryFS::new().into();
let (symbols_opt, _) = get_symbols(
Arc::new(Mutex::new(BTreeMap::new())),
ide_files_layer,
path.as_path(),
LintLevel::None,
)
.unwrap();
let symbols = symbols_opt.unwrap();

let mut fpath = path.clone();
fpath.push("sources/M1.move");
let cpath = dunce::canonicalize(&fpath).unwrap();

let mod_symbols = symbols.file_use_defs.get(&cpath).unwrap();

// struct-typed first part of incomplete dot chain `s.;`
assert_use_def(
mod_symbols,
&symbols,
1,
10,
20,
"M1.move",
9,
12,
"M1.move",
"s: PartialDot::M1::AnotherStruct",
Some((5, 18, "M1.move")),
);
// struct-typed first part of incomplete dot chain `s.another_field.;`
assert_use_def(
mod_symbols,
&symbols,
1,
11,
20,
"M1.move",
9,
12,
"M1.move",
"s: PartialDot::M1::AnotherStruct",
Some((5, 18, "M1.move")),
);
// struct-typed second part of incomplete dot chain `s.another_field.;`
assert_use_def(
mod_symbols,
&symbols,
2,
11,
22,
"M1.move",
6,
8,
"M1.move",
"PartialDot::M1::AnotherStruct\nanother_field: PartialDot::M1::SomeStruct",
Some((1, 18, "M1.move")),
);
// struct-typed second part of incomplete dot chain `s.another_field.` (no `;` but followed by
// `let` on the next line)
assert_use_def(
mod_symbols,
&symbols,
2,
12,
22,
"M1.move",
6,
8,
"M1.move",
"PartialDot::M1::AnotherStruct\nanother_field: PartialDot::M1::SomeStruct",
Some((1, 18, "M1.move")),
);
// struct-typed second part of incomplete dot chain `s.another_field.` (followed by a list of
// parameters and a semi-colon: `s.another_field.(7, 42);`)
assert_use_def(
mod_symbols,
&symbols,
2,
14,
22,
"M1.move",
6,
8,
"M1.move",
"PartialDot::M1::AnotherStruct\nanother_field: PartialDot::M1::SomeStruct",
Some((1, 18, "M1.move")),
);
// struct-typed first part of incomplete dot chain `s.` (no `;` but followed by `}` on the next
// line)
assert_use_def(
mod_symbols,
&symbols,
1,
15,
20,
"M1.move",
9,
12,
"M1.move",
"s: PartialDot::M1::AnotherStruct",
Some((5, 18, "M1.move")),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "PartialDot"
version = "0.0.1"

[dependencies]
MoveStdlib = { local = "../../../move-stdlib/", addr_subst = { "std" = "0x1" } }

[addresses]
PartialDot = "0xCAFE"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module PartialDot::M1 {
public struct SomeStruct has drop {
some_field: u64
}

public struct AnotherStruct has drop {
another_field: SomeStruct
}

fun foo(s: AnotherStruct) {
let _tmp1 = s.;
let _tmp2 = s.another_field.;
let _tmp3 = s.another_field.
let _tmp4 = s; // statement skipped due to unexpected `let`
let _tmp5 = s.another_field.(7, 42);
let _tmp6 = s.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ pub enum ExpDotted_ {
Exp(Box<Exp>),
Dot(Box<ExpDotted>, Name),
Index(Box<ExpDotted>, Spanned<Vec<Exp>>),
DotUnresolved(Loc, Box<ExpDotted>), // dot where Name could not be parsed
}
pub type ExpDotted = Spanned<ExpDotted_>;

Expand Down Expand Up @@ -1734,6 +1735,10 @@ impl AstDebug for ExpDotted_ {
w.comma(&rhs.value, |w, e| e.ast_debug(w));
w.write("]");
}
D::DotUnresolved(_, e) => {
e.ast_debug(w);
w.write(".")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2632,13 +2632,15 @@ fn exp(context: &mut Context, pe: Box<P::Exp>) -> Box<E::Exp> {
EE::UnresolvedError
}
},
pdotted_ @ PE::Dot(_, _) => match exp_dotted(context, Box::new(sp(loc, pdotted_))) {
Some(edotted) => EE::ExpDotted(E::DottedUsage::Use, edotted),
None => {
assert!(context.env().has_errors());
EE::UnresolvedError
pdotted_ @ (PE::Dot(_, _) | PE::DotUnresolved(_, _)) => {
match exp_dotted(context, Box::new(sp(loc, pdotted_))) {
Some(edotted) => EE::ExpDotted(E::DottedUsage::Use, edotted),
None => {
assert!(context.env().has_errors());
EE::UnresolvedError
}
}
},
}

pdotted_ @ PE::Index(_, _) => {
let cur_pkg = context.current_package();
Expand Down Expand Up @@ -2738,6 +2740,7 @@ fn exp_cast(context: &mut Context, in_parens: bool, plhs: Box<P::Exp>, pty: P::T

PE::DotCall(lhs, _, _, _, _)
| PE::Dot(lhs, _)
| PE::DotUnresolved(_, lhs)
| PE::Index(lhs, _)
| PE::Borrow(_, lhs)
| PE::Dereference(lhs) => ambiguous_cast(lhs),
Expand Down Expand Up @@ -2854,7 +2857,9 @@ fn move_or_copy_path_(context: &mut Context, case: PathCase, pe: Box<P::Exp>) ->
return None;
}
}
E::ExpDotted_::Dot(_, _) | E::ExpDotted_::Index(_, _) => {
E::ExpDotted_::Dot(_, _)
| E::ExpDotted_::DotUnresolved(_, _)
| E::ExpDotted_::Index(_, _) => {
let current_package = context.current_package();
context
.env()
Expand Down Expand Up @@ -2892,6 +2897,10 @@ fn exp_dotted(context: &mut Context, pdotted: Box<P::Exp>) -> Option<Box<E::ExpD
.collect::<Vec<_>>();
EE::Index(lhs, sp(argloc, args))
}
PE::DotUnresolved(loc, plhs) => {
let lhs = exp_dotted(context, plhs)?;
EE::DotUnresolved(loc, lhs)
}
pe_ => EE::Exp(exp(context, Box::new(sp(loc, pe_)))),
};
Some(Box::new(sp(loc, edotted_)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ 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) => value_report!(base_exp),
| E::TempBorrow(_, base_exp)
| E::InvalidAccess(base_exp) => value_report!(base_exp),

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

Expand Down Expand Up @@ -746,6 +747,7 @@ fn statement(context: &mut Context, e: &T::Exp) -> Option<ControlFlow> {
| E::ErrorConstant(_)
| E::Move { .. }
| E::Copy { .. }
| E::InvalidAccess(_)
| E::UnresolvedError => value(context, e),

E::Value(_) | E::Unit { .. } => None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ fn value(
context.env.add_diag(ice!((eloc, "ICE unexpanded use")));
error_exp(eloc)
}
E::UnresolvedError => {
E::UnresolvedError | E::InvalidAccess(_) => {
assert!(context.env.has_errors());
make_exp(HE::UnresolvedError)
}
Expand Down Expand Up @@ -2203,6 +2203,7 @@ fn statement(context: &mut Context, block: &mut Block, e: T::Exp) {
| E::Move { .. }
| E::Copy { .. }
| E::UnresolvedError
| E::InvalidAccess(_)
| E::NamedBlock(_, _)) => value_statement(context, block, make_exp(e_)),

E::Value(_) | E::Unit { .. } => (),
Expand Down
5 changes: 5 additions & 0 deletions external-crates/move/crates/move-compiler/src/naming/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ pub enum ExpDotted_ {
Exp(Box<Exp>),
Dot(Box<ExpDotted>, Field),
Index(Box<ExpDotted>, Spanned<Vec<Exp>>),
DotUnresolved(Loc, Box<ExpDotted>), // Dot (and its location) where Field could not be parsed
}
pub type ExpDotted = Spanned<ExpDotted_>;

Expand Down Expand Up @@ -1882,6 +1883,10 @@ impl AstDebug for ExpDotted_ {
w.comma(args, |w, e| e.ast_debug(w));
w.write(")");
}
D::DotUnresolved(_, e) => {
e.ast_debug(w);
w.write(".")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ fn exp(context: &mut Context, sp!(_, e_): &mut N::Exp) {
fn exp_dotted(context: &mut Context, sp!(_, ed_): &mut N::ExpDotted) {
match ed_ {
N::ExpDotted_::Exp(e) => exp(context, e),
N::ExpDotted_::Dot(ed, _) => exp_dotted(context, ed),
N::ExpDotted_::Dot(ed, _) | N::ExpDotted_::DotUnresolved(_, ed) => exp_dotted(context, ed),
N::ExpDotted_::Index(ed, sp!(_, es)) => {
exp_dotted(context, ed);
for e in es {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,9 @@ fn dotted(context: &mut Context, edot: E::ExpDotted) -> Option<N::ExpDotted> {
}
}
E::ExpDotted_::Dot(d, f) => N::ExpDotted_::Dot(Box::new(dotted(context, *d)?), Field(f)),
E::ExpDotted_::DotUnresolved(loc, d) => {
N::ExpDotted_::DotUnresolved(loc, Box::new(dotted(context, *d)?))
}
E::ExpDotted_::Index(inner, args) => {
let args = call_args(context, args);
let inner = Box::new(dotted(context, *inner)?);
Expand Down Expand Up @@ -3652,7 +3655,9 @@ fn remove_unused_bindings_exp_dotted(
) {
match ed_ {
N::ExpDotted_::Exp(e) => remove_unused_bindings_exp(context, used, e),
N::ExpDotted_::Dot(ed, _) => remove_unused_bindings_exp_dotted(context, used, ed),
N::ExpDotted_::Dot(ed, _) | N::ExpDotted_::DotUnresolved(_, ed) => {
remove_unused_bindings_exp_dotted(context, used, ed)
}
N::ExpDotted_::Index(ed, sp!(_, es)) => {
for e in es {
remove_unused_bindings_exp(context, used, e);
Expand Down
6 changes: 6 additions & 0 deletions external-crates/move/crates/move-compiler/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ pub enum Exp_ {
// Internal node marking an error was added to the error list
// This is here so the pass can continue even when an error is hit
UnresolvedError,
// e.X, where X is not a valid tok after dot and cannot be parsed (includes location of the dot)
DotUnresolved(Loc, Box<Exp>),
}
pub type Exp = Spanned<Exp_>;

Expand Down Expand Up @@ -2103,6 +2105,10 @@ impl AstDebug for Exp_ {
w.write(&s.value);
}
E::UnresolvedError => w.write("_|_"),
E::DotUnresolved(_, e) => {
e.ast_debug(w);
w.write(".");
}
}
}
}
Expand Down