Skip to content

Commit

Permalink
remove type requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
dsainati1 committed Jul 20, 2023
1 parent 20fa8e0 commit 776cef3
Show file tree
Hide file tree
Showing 19 changed files with 324 additions and 2,793 deletions.
5 changes: 0 additions & 5 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ listed in no particular order.

Cadence should provide a way to define type aliases.

For example, if a contract interface might declare a type requirement `NFT`,
then all concrete conforming types must provide a concrete type `NFT`.

However, it would be nice to give the type an additional, more useful name.

- `Word128` and `Word256` types

Cadence should provide `Word128` and `Word256` types, just like it provides `UInt128` and `UInt256`
Expand Down
70 changes: 6 additions & 64 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ type CompositeTypeCode struct {
type FunctionWrapper = func(inner FunctionValue) FunctionValue

// WrapperCode contains the "prepared" / "callable" "code"
// for inherited types (interfaces and type requirements).
// for inherited types.
//
// These are "branch" nodes in the call chain, and are function wrappers,
// i.e. they wrap the functions / function wrappers that inherit them.
Expand All @@ -208,11 +208,10 @@ type WrapperCode struct {
}

// TypeCodes is the value which stores the "prepared" / "callable" "code"
// of all composite types, interface types, and type requirements.
// of all composite types and interface types.
type TypeCodes struct {
CompositeCodes map[sema.TypeID]CompositeTypeCode
InterfaceCodes map[sema.TypeID]WrapperCode
TypeRequirementCodes map[sema.TypeID]WrapperCode
CompositeCodes map[sema.TypeID]CompositeTypeCode
InterfaceCodes map[sema.TypeID]WrapperCode
}

func (c TypeCodes) Merge(codes TypeCodes) {
Expand All @@ -227,10 +226,6 @@ func (c TypeCodes) Merge(codes TypeCodes) {
for typeID, code := range codes.InterfaceCodes { //nolint:maprange
c.InterfaceCodes[typeID] = code
}

for typeID, code := range codes.TypeRequirementCodes { //nolint:maprange
c.TypeRequirementCodes[typeID] = code
}
}

type Storage interface {
Expand Down Expand Up @@ -1191,26 +1186,12 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
}
}

// NOTE: First the conditions of the type requirements are evaluated,
// then the conditions of this composite's conformances
//
// Because the conditions are wrappers, they have to be applied
// in reverse order: first the conformances, then the type requirements;
// each conformances and type requirements in reverse order as well.

conformances := compositeType.EffectiveInterfaceConformances()
for i := len(conformances) - 1; i >= 0; i-- {
conformance := conformances[i].InterfaceType
wrapFunctions(interpreter.SharedState.typeCodes.InterfaceCodes[conformance.ID()])
}

typeRequirements := compositeType.TypeRequirements()

for i := len(typeRequirements) - 1; i >= 0; i-- {
typeRequirement := typeRequirements[i]
wrapFunctions(interpreter.SharedState.typeCodes.TypeRequirementCodes[typeRequirement.ID()])
}

interpreter.SharedState.typeCodes.CompositeCodes[compositeType.ID()] = CompositeTypeCode{
DestructorFunction: destructorFunction,
CompositeFunctions: functions,
Expand Down Expand Up @@ -2253,7 +2234,8 @@ func (interpreter *Interpreter) declareInterface(
if nestedCompositeDeclaration.Kind() == common.CompositeKindEvent {
interpreter.declareNonEnumCompositeValue(nestedCompositeDeclaration, lexicalScope)
} else {
interpreter.declareTypeRequirement(nestedCompositeDeclaration, lexicalScope)
// this should be statically prevented in the checker
panic(errors.NewUnreachableError())
}
}
})()
Expand All @@ -2278,46 +2260,6 @@ func (interpreter *Interpreter) declareInterface(
}
}

func (interpreter *Interpreter) declareTypeRequirement(
declaration *ast.CompositeDeclaration,
lexicalScope *VariableActivation,
) {
// Evaluate nested declarations in a new scope, so values
// of nested declarations won't be visible after the containing declaration

(func() {
interpreter.activations.PushNewWithCurrent()
defer interpreter.activations.Pop()

for _, nestedInterfaceDeclaration := range declaration.Members.Interfaces() {
interpreter.declareInterface(nestedInterfaceDeclaration, lexicalScope)
}

for _, nestedCompositeDeclaration := range declaration.Members.Composites() {
interpreter.declareTypeRequirement(nestedCompositeDeclaration, lexicalScope)
}
})()

compositeType := interpreter.Program.Elaboration.CompositeDeclarationType(declaration)
typeID := compositeType.ID()

initializerFunctionWrapper := interpreter.initializerFunctionWrapper(
declaration.Members,
compositeType.ConstructorParameters,
lexicalScope,
)
destructorFunctionWrapper := interpreter.destructorFunctionWrapper(declaration.Members, lexicalScope)
functionWrappers := interpreter.functionWrappers(declaration.Members, lexicalScope)
defaultFunctions := interpreter.defaultFunctions(declaration.Members, lexicalScope)

interpreter.SharedState.typeCodes.TypeRequirementCodes[typeID] = WrapperCode{
InitializerFunctionWrapper: initializerFunctionWrapper,
DestructorFunctionWrapper: destructorFunctionWrapper,
FunctionWrappers: functionWrappers,
Functions: defaultFunctions,
}
}

func (interpreter *Interpreter) initializerFunctionWrapper(
members *ast.Members,
parameters []sema.Parameter,
Expand Down
5 changes: 2 additions & 3 deletions runtime/interpreter/sharedstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ func NewSharedState(config *Config) *SharedState {
allInterpreters: map[common.Location]*Interpreter{},
callStack: &CallStack{},
typeCodes: TypeCodes{
CompositeCodes: map[sema.TypeID]CompositeTypeCode{},
InterfaceCodes: map[sema.TypeID]WrapperCode{},
TypeRequirementCodes: map[sema.TypeID]WrapperCode{},
CompositeCodes: map[sema.TypeID]CompositeTypeCode{},
InterfaceCodes: map[sema.TypeID]WrapperCode{},
},
inStorageIteration: false,
storageMutatedDuringIteration: false,
Expand Down
Loading

0 comments on commit 776cef3

Please sign in to comment.