Skip to content

Commit

Permalink
respond to review
Browse files Browse the repository at this point in the history
  • Loading branch information
dsainati1 committed Nov 4, 2021
1 parent e8b2db4 commit a83cd3f
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 196 deletions.
13 changes: 13 additions & 0 deletions runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,16 @@ func (e NonStorableStaticTypeError) Error() string {
e.Type,
)
}

// InterfaceMissingLocation is reported during interface lookup,
// if an interface is looked up without a location
type InterfaceMissingLocation struct {
QualifiedIdentifier string
}

func (e *InterfaceMissingLocation) Error() string {
return fmt.Sprintf(
"tried to look up interface %s without a location",
e.QualifiedIdentifier,
)
}
36 changes: 20 additions & 16 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2571,34 +2571,37 @@ var converterDeclarations = []valueConverterDeclaration{
},
}

func lookupInterface(interpreter *Interpreter, typeID string) (*sema.InterfaceType, bool) {
func lookupInterface(interpreter *Interpreter, typeID string) (*sema.InterfaceType, error) {
location, qualifiedIdentifier, err := common.DecodeTypeID(typeID)
// if the typeID is invalid, return nil
if err != nil || location == nil {
return nil, false
if err != nil {
return nil, err
}
if location == nil {
return nil, &InterfaceMissingLocation{QualifiedIdentifier: qualifiedIdentifier}
}

typ, err := interpreter.getInterfaceType(location, qualifiedIdentifier)
if err != nil {
return nil, false
return nil, err
}

return typ, true
return typ, nil
}

func lookupComposite(interpreter *Interpreter, typeID string) (*sema.CompositeType, bool) {
func lookupComposite(interpreter *Interpreter, typeID string) (*sema.CompositeType, error) {
location, qualifiedIdentifier, err := common.DecodeTypeID(typeID)
// if the typeID is invalid, return nil
if err != nil {
return nil, false
return nil, err
}

typ, err := interpreter.getCompositeType(location, qualifiedIdentifier, common.TypeID(typeID))
if err != nil {
return nil, false
return nil, err
}

return typ, true
return typ, nil
}

func init() {
Expand Down Expand Up @@ -2657,8 +2660,8 @@ func init() {
func(invocation Invocation) Value {
typeID := invocation.Arguments[0].(*StringValue).Str

composite, ok := lookupComposite(invocation.Interpreter, typeID)
if !ok {
composite, err := lookupComposite(invocation.Interpreter, typeID)
if err != nil {
return NilValue{}
}

Expand All @@ -2677,8 +2680,8 @@ func init() {
func(invocation Invocation) Value {
typeID := invocation.Arguments[0].(*StringValue).Str

interfaceType, ok := lookupInterface(invocation.Interpreter, typeID)
if !ok {
interfaceType, err := lookupInterface(invocation.Interpreter, typeID)
if err != nil {
return NilValue{}
}

Expand Down Expand Up @@ -2738,7 +2741,7 @@ func RestrictedTypeFunction(invocation Invocation) Value {

restrictedIDs.Iterate(func(typeID Value) bool {
restrictionInterface, err := lookupInterface(invocation.Interpreter, typeID.(*StringValue).Str)
if !err {
if err != nil {
ok = false
return true
}
Expand All @@ -2753,13 +2756,14 @@ func RestrictedTypeFunction(invocation Invocation) Value {
}

var semaType sema.Type
var err error

switch typeID := invocation.Arguments[0].(type) {
case NilValue:
semaType = nil
case *SomeValue:
semaType, ok = lookupComposite(invocation.Interpreter, typeID.Value.(*StringValue).Str)
if !ok {
semaType, err = lookupComposite(invocation.Interpreter, typeID.Value.(*StringValue).Str)
if err != nil {
return NilValue{}
}
default:
Expand Down
12 changes: 8 additions & 4 deletions runtime/sema/runtime_type_constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,22 @@ var runtimeTypeConstructors = []*RuntimeTypeConstructor{
{
"DictionaryType",
DictionaryTypeFunctionType,
"Creates a run-time type representing a dictionary type of the given run-time key and value types. Returns nil if the key type is not a valid dictionary key.",
`Creates a run-time type representing a dictionary type of the given run-time key and value types.
Returns nil if the key type is not a valid dictionary key.`,
},

{
"CompositeType",
CompositeTypeFunctionType,
"Creates a run-time type representing the composite type associated with the given type identifier. Returns nil if the identifier does not correspond to any composite type.",
`Creates a run-time type representing the composite type associated with the given type identifier.
Returns nil if the identifier does not correspond to any composite type.`,
},

{
"InterfaceType",
InterfaceTypeFunctionType,
"Creates a run-time type representing the interface type associated with the given type identifier. Returns nil if the identifier does not correspond to any interface type.",
`Creates a run-time type representing the interface type associated with the given type identifier.
Returns nil if the identifier does not correspond to any interface type.`,
},

{
Expand All @@ -201,7 +204,8 @@ var runtimeTypeConstructors = []*RuntimeTypeConstructor{
{
"RestrictedType",
RestrictedTypeFunctionType,
"Creates a run-time type representing a restricted type of the first argument, restricted by the interface identifiers in the second argument. Returns nil if the restriction is not valid.",
`Creates a run-time type representing a restricted type of the first argument, restricted by the interface identifiers in the second argument.
Returns nil if the restriction is not valid.`,
},

{
Expand Down
1 change: 1 addition & 0 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2990,6 +2990,7 @@ func baseFunctionVariable(name string, ty *FunctionType, docString string) *Vari
return &Variable{
Identifier: name,
DeclarationKind: common.DeclarationKindFunction,
ArgumentLabels: ty.ArgumentLabels(),
IsConstant: true,
IsBaseValue: true,
Type: ty,
Expand Down
Loading

0 comments on commit a83cd3f

Please sign in to comment.