Skip to content

Commit

Permalink
Merge pull request #2147 from onflow/attachments-master-sync
Browse files Browse the repository at this point in the history
Sync attachments feature branch with master
  • Loading branch information
dsainati1 authored Nov 18, 2022
2 parents 6c0a461 + b19393f commit f839871
Show file tree
Hide file tree
Showing 74 changed files with 2,669 additions and 683 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ jobs:
- name: Check capabilities of dependencies
run: make check-capabilities

lint-json:
name: Lint JSON
runs-on: ubuntu-latest
steps:
- name: Clone
uses: actions/checkout@v3

- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

semgrep:
name: Semgrep
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions docs/flow-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
{
"title": "Why Use Cadence?",
"tags": ["reference", "patterns"],
"description":"Learn the main benefits of using Cadence for smart contract development."
"href": "/cadence/why-cadence"
"description":"Learn the main benefits of using Cadence for smart contract development.",
"href": "/cadence/why"
},
{
"title": "Hello World Tutorial",
Expand Down
2 changes: 1 addition & 1 deletion fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Fuzz(data []byte) int {
return 0
}

program, err := parser.ParseProgram(data, nil)
program, err := parser.ParseProgram(nil, data, parser.Config{})

if err != nil {
return 0
Expand Down
32 changes: 32 additions & 0 deletions meetings/2022-11-16-Interface-Inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Nov 16, 2022

## Interface Inheritance Meeting Notes

* FLIP: https://github.com/onflow/flips/pull/40

* Forum discussion: https://forum.onflow.org/t/flip-interface-inheritance-in-cadence/3750

* Open Questions:
* Functions with conditions:
* FLIP proposes to order them in a pre-determined order and run them all.
* No overriding is supported (because of security concerns).
* Is overriding needed? Potentially unsafe to do so.

* Default functions: Two main concerns.
* Should allow overriding of default functions?
* Can be a security/safety concern.
* Someone in a middle of an inheritance chain can override a default function, which would change the behavior for downstream contracts.
* One solution is to make default functions to be 'view' only.
* Reduce the depth/impact of security concerns of overriding.
* Still going to need a way to resolve ambiguity. e.g: Two ‘getId()’ view functions are available; which of the two should be called?
* How to resolve ambiguity, when two or more default implementations are available for functions?
* Two potential solutions:
* Ask the user to solve it by overriding the method inside the concrete-type/interface which faces ambiguity
(This is what is proposed in the FLIP).
* Ambiguity resolution of default functions in concrete types also uses the same approach.
See: https://github.com/onflow/cadence/pull/1076#discussion_r675861413
* Order/linearize the default functions and pick the one that is 'closest' to the current interface/concrete type.
* It is 'safe' only if the default functions are view only.
* Might be surprising to the user.
* Already disregarded this option for default functions ambiguity resolution in concrete implementations.
* Need to resolve ambiguity regardless of whether default function overriding is supported or not.
58 changes: 55 additions & 3 deletions runtime/ast/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,20 @@ func CompositeDocument(
return doc
}

// FieldDeclarationFlags

type FieldDeclarationFlags uint8

const (
FieldDeclarationFlagsIsStatic FieldDeclarationFlags = 1 << iota
FieldDeclarationFlagsIsNative
)

// FieldDeclaration

type FieldDeclaration struct {
Access Access
Flags FieldDeclarationFlags
VariableKind VariableKind
Identifier Identifier
TypeAnnotation *TypeAnnotation
Expand All @@ -274,6 +284,8 @@ var _ Declaration = &FieldDeclaration{}
func NewFieldDeclaration(
memoryGauge common.MemoryGauge,
access Access,
isStatic bool,
isNative bool,
variableKind VariableKind,
identifier Identifier,
typeAnnotation *TypeAnnotation,
Expand All @@ -282,8 +294,17 @@ func NewFieldDeclaration(
) *FieldDeclaration {
common.UseMemory(memoryGauge, common.FieldDeclarationMemoryUsage)

var flags FieldDeclarationFlags
if isStatic {
flags |= FieldDeclarationFlagsIsStatic
}
if isNative {
flags |= FieldDeclarationFlagsIsNative
}

return &FieldDeclaration{
Access: access,
Flags: flags,
VariableKind: variableKind,
Identifier: identifier,
TypeAnnotation: typeAnnotation,
Expand Down Expand Up @@ -326,11 +347,17 @@ func (d *FieldDeclaration) DeclarationDocString() string {
func (d *FieldDeclaration) MarshalJSON() ([]byte, error) {
type Alias FieldDeclaration
return json.Marshal(&struct {
Type string
Type string
Flags FieldDeclarationFlags `json:",omitempty"`
IsStatic bool
IsNative bool
*Alias
}{
Type: "FieldDeclaration",
Alias: (*Alias)(d),
Type: "FieldDeclaration",
Alias: (*Alias)(d),
IsStatic: d.IsStatic(),
IsNative: d.IsNative(),
Flags: 0,
})
}

Expand All @@ -347,6 +374,9 @@ func VariableKindDoc(kind VariableKind) prettier.Doc {
}
}

var staticKeywordDoc prettier.Doc = prettier.Text("static")
var nativeKeywordDoc prettier.Doc = prettier.Text("native")

func (d *FieldDeclaration) Doc() prettier.Doc {
identifierTypeDoc := prettier.Concat{
prettier.Text(d.Identifier.Identifier),
Expand All @@ -369,6 +399,20 @@ func (d *FieldDeclaration) Doc() prettier.Doc {
)
}

if d.IsStatic() {
docs = append(
docs,
staticKeywordDoc,
)
}

if d.IsNative() {
docs = append(
docs,
nativeKeywordDoc,
)
}

keywordDoc := VariableKindDoc(d.VariableKind)

if keywordDoc != nil {
Expand Down Expand Up @@ -402,6 +446,14 @@ func (d *FieldDeclaration) String() string {
return Prettier(d)
}

func (d *FieldDeclaration) IsStatic() bool {
return d.Flags&FieldDeclarationFlagsIsStatic != 0
}

func (d *FieldDeclaration) IsNative() bool {
return d.Flags&FieldDeclarationFlagsIsNative != 0
}

// EnumCaseDeclaration

type EnumCaseDeclaration struct {
Expand Down
12 changes: 11 additions & 1 deletion runtime/ast/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestFieldDeclaration_MarshalJSON(t *testing.T) {

decl := &FieldDeclaration{
Access: AccessPublic,
Flags: FieldDeclarationFlagsIsStatic | FieldDeclarationFlagsIsNative,
VariableKind: VariableKindConstant,
Identifier: Identifier{
Identifier: "xyz",
Expand All @@ -61,10 +62,13 @@ func TestFieldDeclaration_MarshalJSON(t *testing.T) {
require.NoError(t, err)

assert.JSONEq(t,
// language=json
`
{
"Type": "FieldDeclaration",
"Access": "AccessPublic",
"IsStatic": true,
"IsNative": true,
"VariableKind": "VariableKindConstant",
"Identifier": {
"Identifier": "xyz",
Expand Down Expand Up @@ -99,13 +103,14 @@ func TestFieldDeclaration_Doc(t *testing.T) {

t.Parallel()

t.Run("with access, with kind", func(t *testing.T) {
t.Run("with access, with kind, with static, with native", func(t *testing.T) {

t.Parallel()

decl := &FieldDeclaration{
Access: AccessPublic,
VariableKind: VariableKindConstant,
Flags: FieldDeclarationFlagsIsNative | FieldDeclarationFlagsIsStatic,
Identifier: Identifier{
Identifier: "xyz",
},
Expand All @@ -125,6 +130,10 @@ func TestFieldDeclaration_Doc(t *testing.T) {
Doc: prettier.Concat{
prettier.Text("pub"),
prettier.Text(" "),
prettier.Text("static"),
prettier.Text(" "),
prettier.Text("native"),
prettier.Text(" "),
prettier.Text("let"),
prettier.Text(" "),
prettier.Group{
Expand Down Expand Up @@ -403,6 +412,7 @@ func TestCompositeDeclaration_MarshalJSON(t *testing.T) {
require.NoError(t, err)

assert.JSONEq(t,
// language=json
`
{
"Type": "CompositeDeclaration",
Expand Down
20 changes: 20 additions & 0 deletions runtime/ast/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,8 @@ var functionExpressionEmptyBlockDoc prettier.Doc = prettier.Text(" {}")

func FunctionDocument(
access Access,
isStatic bool,
isNative bool,
includeKeyword bool,
identifier string,
parameterList *ParameterList,
Expand Down Expand Up @@ -1426,6 +1428,22 @@ func FunctionDocument(
)
}

if isStatic {
doc = append(
doc,
staticKeywordDoc,
prettier.Space,
)
}

if isNative {
doc = append(
doc,
nativeKeywordDoc,
prettier.Space,
)
}

if includeKeyword {
doc = append(
doc,
Expand Down Expand Up @@ -1465,6 +1483,8 @@ func FunctionDocument(
func (e *FunctionExpression) Doc() prettier.Doc {
return FunctionDocument(
AccessNotSpecified,
false,
false,
true,
"",
e.ParameterList,
Expand Down
45 changes: 41 additions & 4 deletions runtime/ast/function_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ import (
"github.com/onflow/cadence/runtime/common"
)

type FunctionDeclarationFlags uint8

const (
FunctionDeclarationFlagsIsStatic FunctionDeclarationFlags = 1 << iota
FunctionDeclarationFlagsIsNative
)

type FunctionDeclaration struct {
Access Access
Flags FunctionDeclarationFlags
Identifier Identifier
ParameterList *ParameterList
ReturnTypeAnnotation *TypeAnnotation
Expand All @@ -43,6 +51,8 @@ var _ Statement = &FunctionDeclaration{}
func NewFunctionDeclaration(
gauge common.MemoryGauge,
access Access,
isStatic bool,
isNative bool,
identifier Identifier,
parameterList *ParameterList,
returnTypeAnnotation *TypeAnnotation,
Expand All @@ -52,8 +62,17 @@ func NewFunctionDeclaration(
) *FunctionDeclaration {
common.UseMemory(gauge, common.FunctionDeclarationMemoryUsage)

var flags FunctionDeclarationFlags
if isStatic {
flags |= FunctionDeclarationFlagsIsStatic
}
if isNative {
flags |= FunctionDeclarationFlagsIsNative
}

return &FunctionDeclaration{
Access: access,
Flags: flags,
Identifier: identifier,
ParameterList: parameterList,
ReturnTypeAnnotation: returnTypeAnnotation,
Expand Down Expand Up @@ -126,6 +145,8 @@ func (d *FunctionDeclaration) DeclarationDocString() string {
func (d *FunctionDeclaration) Doc() prettier.Doc {
return FunctionDocument(
d.Access,
d.IsStatic(),
d.IsNative(),
true,
d.Identifier.Identifier,
d.ParameterList,
Expand All @@ -137,20 +158,34 @@ func (d *FunctionDeclaration) Doc() prettier.Doc {
func (d *FunctionDeclaration) MarshalJSON() ([]byte, error) {
type Alias FunctionDeclaration
return json.Marshal(&struct {
Type string
Type string
IsStatic bool
IsNative bool
Flags FunctionDeclarationFlags `json:",omitempty"`
Range
*Alias
}{
Type: "FunctionDeclaration",
Range: NewUnmeteredRangeFromPositioned(d),
Alias: (*Alias)(d),
Type: "FunctionDeclaration",
Range: NewUnmeteredRangeFromPositioned(d),
IsStatic: d.IsStatic(),
IsNative: d.IsNative(),
Alias: (*Alias)(d),
Flags: 0,
})
}

func (d *FunctionDeclaration) String() string {
return Prettier(d)
}

func (d *FunctionDeclaration) IsStatic() bool {
return d.Flags&FunctionDeclarationFlagsIsStatic != 0
}

func (d *FunctionDeclaration) IsNative() bool {
return d.Flags&FunctionDeclarationFlagsIsNative != 0
}

// SpecialFunctionDeclaration

type SpecialFunctionDeclaration struct {
Expand Down Expand Up @@ -218,6 +253,8 @@ func (d *SpecialFunctionDeclaration) DeclarationDocString() string {
func (d *SpecialFunctionDeclaration) Doc() prettier.Doc {
return FunctionDocument(
d.FunctionDeclaration.Access,
d.FunctionDeclaration.IsStatic(),
d.FunctionDeclaration.IsNative(),
false,
d.Kind.Keywords(),
d.FunctionDeclaration.ParameterList,
Expand Down
Loading

0 comments on commit f839871

Please sign in to comment.