Skip to content

Commit

Permalink
Address comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
RZhang05 committed Dec 17, 2024
1 parent 5689128 commit ad709cb
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 32 deletions.
7 changes: 6 additions & 1 deletion ast/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func TestImportDeclaration_MarshalJSON(t *testing.T) {
Pos: Position{Offset: 1, Line: 2, Column: 3},
},
},
Aliases: map[string]string{
"foo": "bar",
},
Location: common.StringLocation("test"),
LocationPos: Position{Offset: 4, Line: 5, Column: 6},
Range: Range{
Expand All @@ -63,7 +66,9 @@ func TestImportDeclaration_MarshalJSON(t *testing.T) {
"EndPos": {"Offset": 3, "Line": 2, "Column": 5}
}
],
"Aliases": null,
"Aliases": {
"foo": "bar"
},
"Location": {
"Type": "StringLocation",
"String": "test"
Expand Down
92 changes: 92 additions & 0 deletions interpreter/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,95 @@ func TestInterpretImportWithAlias(t *testing.T) {
value,
)
}

func TestInterpretImportAliasGetType(t *testing.T) {

t.Parallel()

address := common.MustBytesToAddress([]byte{0x1})

importedChecker, err := ParseAndCheckWithOptions(t,
`
access(all) struct Foo {
}
`,
ParseAndCheckOptions{
Location: common.AddressLocation{
Address: address,
Name: "",
},
},
)
require.NoError(t, err)

importingChecker, err := ParseAndCheckWithOptions(t,
`
import Foo as Bar from 0x1
access(all) fun test(): String {
var bar: Bar = Bar()
return bar.getType().identifier
}
`,
ParseAndCheckOptions{
Config: &sema.Config{
LocationHandler: func(identifiers []ast.Identifier, location common.Location) (result []sema.ResolvedLocation, err error) {

for _, identifier := range identifiers {
result = append(result, sema.ResolvedLocation{
Location: common.AddressLocation{
Address: location.(common.AddressLocation).Address,
Name: identifier.Identifier,
},
Identifiers: []ast.Identifier{
identifier,
},
})
}
return
},
ImportHandler: func(checker *sema.Checker, importedLocation common.Location, _ ast.Range) (sema.Import, error) {
return sema.ElaborationImport{
Elaboration: importedChecker.Elaboration,
}, nil
},
},
},
)
require.NoError(t, err)

storage := newUnmeteredInMemoryStorage()

inter, err := interpreter.NewInterpreter(
interpreter.ProgramFromChecker(importingChecker),
importingChecker.Location,
&interpreter.Config{
Storage: storage,
ImportLocationHandler: func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
program := interpreter.ProgramFromChecker(importedChecker)
subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}

return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
},
},
)
require.NoError(t, err)

err = inter.Interpret()
require.NoError(t, err)

value, err := inter.Invoke("test")
require.NoError(t, err)

AssertValuesEqual(
t,
inter,
interpreter.NewUnmeteredStringValue("A.0000000000000001.Foo"),
value,
)
}
6 changes: 3 additions & 3 deletions interpreter/interpreter_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (interpreter *Interpreter) VisitImportDeclaration(declaration *ast.ImportDe
resolvedLocations := interpreter.Program.Elaboration.ImportDeclarationsResolvedLocations(declaration)

for _, resolvedLocation := range resolvedLocations {
interpreter.importResolvedLocation(resolvedLocation, &declaration.Aliases)
interpreter.importResolvedLocation(resolvedLocation, declaration.Aliases)
}

return nil
}

func (interpreter *Interpreter) importResolvedLocation(resolvedLocation sema.ResolvedLocation, aliases *map[string]string) {
func (interpreter *Interpreter) importResolvedLocation(resolvedLocation sema.ResolvedLocation, aliases map[string]string) {
config := interpreter.SharedState.Config

// tracing
Expand All @@ -63,7 +63,7 @@ func (interpreter *Interpreter) importResolvedLocation(resolvedLocation sema.Res
variables = make(map[string]Variable, identifierLength)
for _, identifier := range resolvedLocation.Identifiers {
name := identifier.Identifier
alias, ok := (*aliases)[name]
alias, ok := aliases[name]
if ok {
name = alias
}
Expand Down
2 changes: 1 addition & 1 deletion parser/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ func parsePragmaDeclaration(p *parser) (*ast.PragmaDeclaration, error) {
//
// importDeclaration :
// 'import'
// ( identifier (as identifier)? (',' identifier (as identifier)?)* 'from' )?
// ( identifier ('as' identifier)? (',' identifier ('as' identifier)?)* 'from' )?
// ( string | hexadecimalLiteral | identifier )
func parseImportDeclaration(p *parser) (*ast.ImportDeclaration, error) {

Expand Down
8 changes: 4 additions & 4 deletions sema/check_import_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (checker *Checker) declareImportDeclaration(declaration *ast.ImportDeclarat
checker.Elaboration.SetImportDeclarationsResolvedLocations(declaration, resolvedLocations)

for _, resolvedLocation := range resolvedLocations {
checker.importResolvedLocation(resolvedLocation, locationRange, &declaration.Aliases)
checker.importResolvedLocation(resolvedLocation, locationRange, declaration.Aliases)
}
}

Expand All @@ -113,7 +113,7 @@ func (checker *Checker) resolveLocation(identifiers []ast.Identifier, location c
return locationHandler(identifiers, location)
}

func (checker *Checker) importResolvedLocation(resolvedLocation ResolvedLocation, locationRange ast.Range, aliases *map[string]string) {
func (checker *Checker) importResolvedLocation(resolvedLocation ResolvedLocation, locationRange ast.Range, aliases map[string]string) {

// First, get the Import for the resolved location

Expand Down Expand Up @@ -304,7 +304,7 @@ func (checker *Checker) importElements(
valueActivations *VariableActivations,
requestedIdentifiers []ast.Identifier,
availableElements *StringImportElementOrderedMap,
aliases *map[string]string,
aliases map[string]string,
importValues bool,
) (
found map[ast.Identifier]bool,
Expand All @@ -329,7 +329,7 @@ func (checker *Checker) importElements(
if !ok {
continue
}
alias, ok := (*aliases)[name]
alias, ok := aliases[name]
if ok {
name = alias
}
Expand Down
45 changes: 22 additions & 23 deletions sema/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,19 +833,20 @@ func TestCheckImportAlias(t *testing.T) {

importedChecker, err := ParseAndCheckWithOptions(t,
`
access(all) contract Foo {
access(all) let x: [Int]
access(all) contract Foo {
access(all) let x: [Int]
access(all) fun answer(): Int {
return 42
}
access(all) fun answer(): Int {
return 42
}
access(all) struct Bar {}
access(all) struct Bar {}
init() {
self.x = []
}
}`,
init() {
self.x = []
}
}
`,
ParseAndCheckOptions{
Location: ImportedLocation,
},
Expand Down Expand Up @@ -881,19 +882,20 @@ func TestCheckImportAlias(t *testing.T) {

importedChecker, err := ParseAndCheckWithOptions(t,
`
access(all) contract Foo {
access(all) let x: [Int]
access(all) contract Foo {
access(all) let x: [Int]
access(all) fun answer(): Int {
return 42
}
access(all) fun answer(): Int {
return 42
}
access(all) struct Bar {}
access(all) struct Bar {}
init() {
self.x = []
}
}`,
init() {
self.x = []
}
}
`,
ParseAndCheckOptions{
Location: ImportedLocation,
},
Expand Down Expand Up @@ -937,7 +939,6 @@ func TestCheckImportAlias(t *testing.T) {
access(all) fun b(): Int {
return 50
}
`,
ParseAndCheckOptions{
Location: ImportedLocation,
Expand Down Expand Up @@ -980,7 +981,6 @@ func TestCheckImportAlias(t *testing.T) {
access(all) fun a(): Int {
return 42
}
`,
ParseAndCheckOptions{
Location: ImportedLocation,
Expand Down Expand Up @@ -1022,7 +1022,6 @@ func TestCheckImportAlias(t *testing.T) {
access(all) fun a(): Int {
return 42
}
`,
ParseAndCheckOptions{
Location: ImportedLocation,
Expand Down

0 comments on commit ad709cb

Please sign in to comment.