Skip to content

Commit

Permalink
Merge pull request #3642 from onflow/supun/cleanup-attachments-flag
Browse files Browse the repository at this point in the history
Remove attachments enabling flag
  • Loading branch information
SupunS authored Oct 23, 2024
2 parents 85f46f7 + 0e1b932 commit af8e417
Show file tree
Hide file tree
Showing 24 changed files with 53 additions and 239 deletions.
1 change: 0 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func DefaultCheckerConfig(
Elaboration: importedChecker.Elaboration,
}, nil
},
AttachmentsEnabled: true,
}
}

Expand Down
2 changes: 0 additions & 2 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ type Config struct {
ResourceOwnerChangeHandlerEnabled bool
// CoverageReport enables and collects coverage reporting metrics
CoverageReport *CoverageReport
// AttachmentsEnabled specifies if attachments are enabled
AttachmentsEnabled bool
// LegacyContractUpgradeEnabled enabled specifies whether to use the old parser when parsing an old contract
LegacyContractUpgradeEnabled bool
// ContractUpdateTypeRemovalEnabled specifies if type removal is enabled in contract updates
Expand Down
1 change: 0 additions & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ func (e *interpreterEnvironment) newCheckerConfig() *sema.Config {
LocationHandler: e.ResolveLocation,
ImportHandler: e.resolveImport,
CheckHandler: e.newCheckHandler(),
AttachmentsEnabled: e.config.AttachmentsEnabled,
}
}

Expand Down
6 changes: 0 additions & 6 deletions sema/check_attach_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (

func (checker *Checker) VisitAttachExpression(expression *ast.AttachExpression) Type {

if !checker.Config.AttachmentsEnabled {
checker.report(&AttachmentsNotEnabledError{
Range: ast.NewRangeFromPositioned(checker.memoryGauge, expression),
})
}

attachment := expression.Attachment
baseExpression := expression.Base

Expand Down
7 changes: 0 additions & 7 deletions sema/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,6 @@ func (checker *Checker) VisitAttachmentDeclaration(declaration *ast.AttachmentDe
}

func (checker *Checker) visitAttachmentDeclaration(declaration *ast.AttachmentDeclaration) (_ struct{}) {

if !checker.Config.AttachmentsEnabled {
checker.report(&AttachmentsNotEnabledError{
Range: ast.NewRangeFromPositioned(checker.memoryGauge, declaration),
})
}

checker.visitCompositeLikeDeclaration(declaration)
attachmentType := checker.Elaboration.CompositeDeclarationType(declaration)
checker.checkAttachmentMembersAccess(attachmentType)
Expand Down
6 changes: 0 additions & 6 deletions sema/check_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,6 @@ func (checker *Checker) checkTypeIndexingExpression(

targetExpression := indexExpression.TargetExpression

if !checker.Config.AttachmentsEnabled {
checker.report(&AttachmentsNotEnabledError{
Range: ast.NewRangeFromPositioned(checker.memoryGauge, indexExpression),
})
}

expressionType := ast.ExpressionAsType(indexExpression.IndexingExpression)
if expressionType == nil {
return InvalidType
Expand Down
6 changes: 0 additions & 6 deletions sema/check_remove_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (

func (checker *Checker) VisitRemoveStatement(statement *ast.RemoveStatement) (_ struct{}) {

if !checker.Config.AttachmentsEnabled {
checker.report(&AttachmentsNotEnabledError{
Range: ast.NewRangeFromPositioned(checker.memoryGauge, statement),
})
}

nominalType := checker.convertNominalType(statement.Attachment)
base := checker.VisitExpression(statement.Value, statement, nil)
checker.checkUnusedExpressionResourceLoss(base, statement.Value)
Expand Down
2 changes: 0 additions & 2 deletions sema/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,4 @@ type Config struct {
AllowNativeDeclarations bool
// AllowStaticDeclarations determines if declarations may be static
AllowStaticDeclarations bool
// AttachmentsEnabled determines if attachments are enabled
AttachmentsEnabled bool
}
16 changes: 0 additions & 16 deletions sema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4639,22 +4639,6 @@ func (e *InvalidTypeIndexingError) Error() string {
)
}

// AttachmentsNotEnabledError
type AttachmentsNotEnabledError struct {
ast.Range
}

var _ SemanticError = &AttachmentsNotEnabledError{}
var _ errors.UserError = &AttachmentsNotEnabledError{}

func (*AttachmentsNotEnabledError) isSemanticError() {}

func (*AttachmentsNotEnabledError) IsUserError() {}

func (e *AttachmentsNotEnabledError) Error() string {
return "attachments are not enabled and cannot be used in this environment"
}

// InvalidAttachmentEntitlementError
type InvalidAttachmentEntitlementError struct {
Attachment *CompositeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func testContractUpdate(t *testing.T, oldCode string, newCode string) error {
utils.TestLocation,
nil,
&sema.Config{
AccessCheckMode: sema.AccessCheckModeStrict,
AttachmentsEnabled: true,
AccessCheckMode: sema.AccessCheckModeStrict,
})
require.NoError(t, err)

Expand Down Expand Up @@ -130,8 +129,7 @@ func parseAndCheckPrograms(
location,
nil,
&sema.Config{
AccessCheckMode: sema.AccessCheckModeStrict,
AttachmentsEnabled: true,
AccessCheckMode: sema.AccessCheckModeStrict,
},
)

Expand Down Expand Up @@ -171,7 +169,6 @@ func parseAndCheckPrograms(

return
},
AttachmentsEnabled: true,
})
require.NoError(t, err)

Expand Down
14 changes: 7 additions & 7 deletions tests/attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestRuntimeAccountAttachmentSaveAndLoad(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

var logs []string
var events []string
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestRuntimeAccountAttachmentExportFailure(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

logs := make([]string, 0)
events := make([]string, 0)
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestRuntimeAccountAttachmentExport(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

var logs []string
var events []string
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestRuntimeAccountAttachedExport(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

var logs []string
var events []string
Expand Down Expand Up @@ -411,7 +411,7 @@ func TestRuntimeAccountAttachmentSaveAndBorrow(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

var logs []string
var events []string
Expand Down Expand Up @@ -525,7 +525,7 @@ func TestRuntimeAccountAttachmentCapability(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

var logs []string
var events []string
Expand Down Expand Up @@ -666,7 +666,7 @@ func TestRuntimeAttachmentStorage(t *testing.T) {
address := common.MustBytesToAddress([]byte{0x1})

newRuntime := func() (TestInterpreterRuntime, *TestRuntimeInterface) {
runtime := NewTestInterpreterRuntimeWithAttachments()
runtime := NewTestInterpreterRuntime()

accountCodes := map[common.Location][]byte{}

Expand Down
3 changes: 1 addition & 2 deletions tests/checker/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,7 @@ func TestCheckAccessModifierGlobalCompositeDeclaration(t *testing.T) {
),
ParseAndCheckOptions{
Config: &sema.Config{
AccessCheckMode: checkMode,
AttachmentsEnabled: true,
AccessCheckMode: checkMode,
},
},
)
Expand Down
92 changes: 0 additions & 92 deletions tests/checker/attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3816,7 +3816,6 @@ func TestCheckAttachmentsExternalMutation(t *testing.T) {
`,
ParseAndCheckOptions{Config: &sema.Config{
SuggestionsEnabled: true,
AttachmentsEnabled: true,
}},
)

Expand Down Expand Up @@ -3896,7 +3895,6 @@ func TestCheckAttachmentsExternalMutation(t *testing.T) {
`,
ParseAndCheckOptions{Config: &sema.Config{
SuggestionsEnabled: true,
AttachmentsEnabled: true,
}},
)

Expand Down Expand Up @@ -3970,7 +3968,6 @@ func TestCheckAttachmentsExternalMutation(t *testing.T) {
`,
ParseAndCheckOptions{Config: &sema.Config{
SuggestionsEnabled: true,
AttachmentsEnabled: true,
}},
)

Expand Down Expand Up @@ -4389,95 +4386,6 @@ func TestCheckAttachmentsResourceReference(t *testing.T) {
})
}

func TestCheckAttachmentsNotEnabled(t *testing.T) {

t.Parallel()

parseAndCheckWithoutAttachments := func(t *testing.T, code string) (*sema.Checker, error) {
return ParseAndCheckWithOptions(t, code, ParseAndCheckOptions{})
}

t.Run("declaration", func(t *testing.T) {

t.Parallel()

_, err := parseAndCheckWithoutAttachments(t,
`
struct S {}
attachment Test for S {}`,
)

errs := RequireCheckerErrors(t, err, 1)
assert.IsType(t, &sema.AttachmentsNotEnabledError{}, errs[0])
})

t.Run("attach", func(t *testing.T) {

t.Parallel()

_, err := parseAndCheckWithoutAttachments(t,
`
struct S {}
let s = attach A() to S()
`,
)

errs := RequireCheckerErrors(t, err, 2)
assert.IsType(t, &sema.AttachmentsNotEnabledError{}, errs[0])
assert.IsType(t, &sema.NotDeclaredError{}, errs[1])
})

t.Run("remove", func(t *testing.T) {

t.Parallel()

_, err := parseAndCheckWithoutAttachments(t,
`
struct S {}
fun foo() {
remove A from S()
}
`,
)

errs := RequireCheckerErrors(t, err, 2)
assert.IsType(t, &sema.AttachmentsNotEnabledError{}, errs[0])
assert.IsType(t, &sema.NotDeclaredError{}, errs[1])
})

t.Run("type indexing", func(t *testing.T) {

t.Parallel()

_, err := parseAndCheckWithoutAttachments(t,
`
struct S {}
attachment A for S {}
let s = S()
let r = s[A]
`,
)

errs := RequireCheckerErrors(t, err, 2)
assert.IsType(t, &sema.AttachmentsNotEnabledError{}, errs[0])
assert.IsType(t, &sema.AttachmentsNotEnabledError{}, errs[1])
})

t.Run("regular indexing ok", func(t *testing.T) {

t.Parallel()

_, err := parseAndCheckWithoutAttachments(t,
`
let x = [1, 2, 3]
let y = x[2]
`,
)

require.NoError(t, err)
})
}

func TestCheckAttachmentForEachAttachment(t *testing.T) {

t.Parallel()
Expand Down
1 change: 0 additions & 1 deletion tests/checker/storable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestCheckStorable(t *testing.T) {
BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation {
return baseValueActivation
},
AttachmentsEnabled: true,
},
},
)
Expand Down
7 changes: 1 addition & 6 deletions tests/checker/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ import (
)

func ParseAndCheck(t testing.TB, code string) (*sema.Checker, error) {
return ParseAndCheckWithOptions(t, code, ParseAndCheckOptions{
// allow attachments is on by default for testing purposes
Config: &sema.Config{
AttachmentsEnabled: true,
},
})
return ParseAndCheckWithOptions(t, code, ParseAndCheckOptions{})
}

type ParseAndCheckOptions struct {
Expand Down
2 changes: 1 addition & 1 deletion tests/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5270,7 +5270,7 @@ func TestRuntimeNestedStructArgPassing(t *testing.T) {
func TestRuntimeDestroyedResourceReferenceExport(t *testing.T) {
t.Parallel()

rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()

script := []byte(`
access(all) resource S {}
Expand Down
8 changes: 4 additions & 4 deletions tests/entitlements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestRuntimeAccountEntitlementAttachment(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()
accountCodes := map[Location][]byte{}

deployTx := DeploymentTransaction("Test", []byte(`
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestRuntimeAccountEntitlementCapabilityCasting(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()
accountCodes := map[Location][]byte{}

deployTx := DeploymentTransaction("Test", []byte(`
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestRuntimeAccountEntitlementCapabilityDictionary(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()
accountCodes := map[Location][]byte{}

deployTx := DeploymentTransaction("Test", []byte(`
Expand Down Expand Up @@ -722,7 +722,7 @@ func TestRuntimeAccountEntitlementGenericCapabilityDictionary(t *testing.T) {
t.Parallel()

storage := NewTestLedger(nil, nil)
rt := NewTestInterpreterRuntimeWithAttachments()
rt := NewTestInterpreterRuntime()
accountCodes := map[Location][]byte{}

deployTx := DeploymentTransaction("Test", []byte(`
Expand Down
Loading

0 comments on commit af8e417

Please sign in to comment.