Skip to content

Commit

Permalink
Tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
awelc committed Aug 1, 2024
1 parent a522590 commit 2479eb1
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,14 @@ fn module_use(
}
}
}
P::ModuleUse::Partial { .. } => (), // no members or aliases to process
P::ModuleUse::Partial { .. } => {
let mident = module_ident(&mut context.defn_context, in_mident);
if !context.defn_context.module_members.contains_key(&mident) {
context.env().add_diag(unbound_module(&mident));
return;
};
add_module_alias!(mident, mident.value.module.0)
}
}
}

Expand Down
79 changes: 46 additions & 33 deletions external-crates/move/crates/move-compiler/src/parser/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,12 +1499,8 @@ fn parse_sequence(context: &mut Context) -> Result<Sequence, Box<Diagnostic>> {
let mut uses = vec![];
while context.tokens.peek() == Tok::Use {
let start_loc = context.tokens.start_loc();
uses.push(parse_use_decl(
vec![],
start_loc,
Modifiers::empty(),
context,
)?);
let tmp = parse_use_decl(vec![], start_loc, Modifiers::empty(), context)?;
uses.push(tmp);
}

let mut seq: Vec<SequenceItem> = vec![];
Expand Down Expand Up @@ -4137,7 +4133,7 @@ fn parse_use_decl(
start_loc,
" after an address in a use declaration",
) {
context.env.add_diag(*diag);
context.add_diag(*diag);
Use::Partial {
package: address,
colon_colon: None,
Expand All @@ -4159,6 +4155,9 @@ fn parse_use_decl(
let (name, _, use_) = parse_use_module(ctxt)?;
Ok((name, use_))
};
// add `;` to stop set to limit number of eaten tokens if the list is parsed
// incorrectly
context.stop_set.add(Tok::Semicolon);
let use_decls = parse_comma_list(
context,
Tok::LBrace,
Expand All @@ -4167,7 +4166,7 @@ fn parse_use_decl(
parse_inner,
"a module use clause",
);
if use_decls.is_empty() && module_uses_to_parse_exist {
let use_ = if use_decls.is_empty() && module_uses_to_parse_exist {
// we failed to parse a non-empty list
Use::Partial {
package: address,
Expand All @@ -4176,35 +4175,49 @@ fn parse_use_decl(
}
} else {
Use::NestedModuleUses(address, use_decls)
}
};
context.stop_set.remove(Tok::Semicolon);
use_
}
_ => match parse_use_module(context) {
Ok((name, end_loc, use_)) => {
let loc =
make_loc(context.tokens.file_hash(), address_start_loc, end_loc);
let module_ident = sp(
loc,
ModuleIdent_ {
address,
module: name,
},
);
Use::ModuleUse(module_ident, use_)
}
Err(diag) => {
context.env.add_diag(*diag);
Use::Partial {
package: address,
colon_colon: Some(colon_colon_loc),
opening_brace: None,
_ => {
// add `;` to stop set to limit number of eaten tokens if the module use is
// parsed incorrectly
context.stop_set.add(Tok::Semicolon);
let use_ = match parse_use_module(context) {
Ok((name, end_loc, use_)) => {
let loc = make_loc(
context.tokens.file_hash(),
address_start_loc,
end_loc,
);
let module_ident = sp(
loc,
ModuleIdent_ {
address,
module: name,
},
);
Use::ModuleUse(module_ident, use_)
}
}
},
Err(diag) => {
context.add_diag(*diag);
Use::Partial {
package: address,
colon_colon: Some(colon_colon_loc),
opening_brace: None,
}
}
};
context.stop_set.remove(Tok::Semicolon);
use_
}
}
}
}
};
consume_token(context.tokens, Tok::Semicolon)?;
if let Err(diag) = consume_token(context.tokens, Tok::Semicolon) {
context.add_diag(*diag);
}
let end_loc = context.tokens.previous_end_loc();
let loc = make_loc(context.tokens.file_hash(), start_loc, end_loc);
Ok(UseDecl {
Expand All @@ -4229,7 +4242,7 @@ fn parse_use_module(
(None, Tok::ColonColon) => {
let colon_colon_loc = context.tokens.current_token_loc();
if let Err(diag) = consume_token(context.tokens, Tok::ColonColon) {
context.env.add_diag(*diag);
context.add_diag(*diag);
ModuleUse::Partial {
colon_colon: None,
opening_brace: None,
Expand Down Expand Up @@ -4267,7 +4280,7 @@ fn parse_use_module(
_ => match parse_use_member(context) {
Ok(m) => ModuleUse::Members(vec![m]),
Err(diag) => {
context.env.add_diag(*diag);
context.add_diag(*diag);
ModuleUse::Partial {
colon_colon: Some(colon_colon_loc),
opening_brace: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:5:9
5 │ let _tmp = 42; // reset parser to see if the next line compiles
│ ^^^
│ │
│ Unexpected 'let'
│ Expected an identifier

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:11:9
11 │ let _tmp = 42; // reset parser to see if the next line compiles
│ ^^^
│ │
│ Unexpected 'let'
│ Expected ',' or '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:11:22
10 │ use a::m2::{foo
│ - To match this '{'
11 │ let _tmp = 42; // reset parser to see if the next line compiles
│ ^ Expected '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:17:9
17 │ let _tmp = 42; // reset parser to see if the next lines compile
│ ^^^
│ │
│ Unexpected 'let'
│ Expected ',' or '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:17:22
16 │ use a::m2::{foo, bar
│ - To match this '{'
17 │ let _tmp = 42; // reset parser to see if the next lines compile
│ ^ Expected '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:24:9
24 │ let _tmp = 42; // reset parser to see if the next lines compile
│ ^^^
│ │
│ Unexpected 'let'
│ Expected ',' or '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:24:22
23 │ use a::{m2::{foo, bar
│ - To match this '{'
24 │ let _tmp = 42; // reset parser to see if the next lines compile
│ ^ Expected '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:32:9
32 │ let _tmp = 42; // reset parser to see if the next lines compile
│ ^^^
│ │
│ Unexpected 'let'
│ Expected ',' or '}'

error[E01002]: unexpected token
┌─ tests/move_2024/ide_mode/use_incomplete.move:32:22
31 │ use a::{m2::{foo, bar}, m3
│ - To match this '{'
32 │ let _tmp = 42; // reset parser to see if the next lines compile
│ ^ Expected '}'

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module a::m {

public fun test1() {
use a::m2::
let _tmp = 42; // reset parser to see if the next line compiles
m2::foo();
}

public fun test2() {
use a::m2::{foo
let _tmp = 42; // reset parser to see if the next line compiles
foo();
}

public fun test3() {
use a::m2::{foo, bar
let _tmp = 42; // reset parser to see if the next lines compile
foo();
bar();
}

public fun test4() {
use a::{m2::{foo, bar
let _tmp = 42; // reset parser to see if the next lines compile
foo();
bar();

}

public fun test5() {
use a::{m2::{foo, bar}, m3
let _tmp = 42; // reset parser to see if the next lines compile
m3::baz();
foo();
bar();
}
}

module a::m2 {

public fun foo() {}

public fun bar() {}
}

module a::m3 {

public fun baz() {}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E01002]: unexpected token
┌─ tests/move_check/parser/use_module_member_invalid_missing_close_brace.move:6:5
4 │ use 0x1::X::{S as XS
│ - To match this '{'
4 │ use 0x42::M::{S as XS
- To match this '{'
5 │
6 │ fun foo() {
6 │ fun foo() {}
│ ^ Expected '}'

error[E01002]: unexpected token
┌─ tests/move_check/parser/use_module_member_invalid_missing_close_brace.move:6:5
6 │ fun foo() {
6 │ fun foo() {}
│ ^^^
│ │
│ Unexpected 'fun'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

module 0x42::M {

use 0x1::X::{S as XS
use 0x42::M::{S as XS

fun foo() {
fun foo() {}

}
struct S has drop {}

fun bar(_p: XS) {}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error[E01002]: unexpected token
┌─ tests/move_check/parser/use_module_member_invalid_missing_semicolon.move:5:1
┌─ tests/move_check/parser/use_module_member_invalid_missing_semicolon.move:6:5
5 │ }
│ ^
│ │
│ Unexpected '}'
│ Expected ';'
6 fun foo() {}
^^^
Unexpected 'fun'
Expected ';'

Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

module 0x42::M {

use 0x1::X::{S as XS,}
use 0x42::M::{S as XS,}

fun foo() {}

struct S has drop {}

fun bar(_p: XS) {}
}

0 comments on commit 2479eb1

Please sign in to comment.