Skip to content

Commit

Permalink
Merge pull request #221 from franklinsch/public-struct-args
Browse files Browse the repository at this point in the history
Disallow parameters of public functions to be of dynamic types
  • Loading branch information
franklinsch authored May 25, 2018
2 parents 943dd15 + 2e55acf commit 56cdb3f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Sources/SemanticAnalyzer/SemanticAnalyzer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ public struct SemanticAnalyzer: ASTPass {
diagnostics.append(.payableFunctionDoesNotHavePayableValueParameter(functionDeclaration))
}
}

if functionDeclaration.isPublic {
let dynamicParameters = functionDeclaration.parameters.filter { $0.type.rawType.isDynamicType && !$0.isImplicit }
if !dynamicParameters.isEmpty {
diagnostics.append(.useOfDynamicParamaterInFunctionDeclaration(functionDeclaration, dynamicParameters: dynamicParameters))
}
}

let statements = functionDeclaration.body

Expand Down
5 changes: 5 additions & 0 deletions Sources/SemanticAnalyzer/SemanticError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ extension Diagnostic {
static func payableFunctionDoesNotHavePayableValueParameter(_ functionDeclaration: FunctionDeclaration) -> Diagnostic {
return Diagnostic(severity: .error, sourceLocation: functionDeclaration.sourceLocation, message: "'\(functionDeclaration.identifier.name)' is declared @payable but doesn't have an implicit parameter of a currency type")
}

static func useOfDynamicParamaterInFunctionDeclaration(_ functionDeclaration: FunctionDeclaration, dynamicParameters: [Parameter]) -> Diagnostic {
let notes = dynamicParameters.map { Diagnostic(severity: .note, sourceLocation: $0.sourceLocation, message: "\($0.identifier.name) cannot be used as a parameter") }
return Diagnostic(severity: .error, sourceLocation: functionDeclaration.sourceLocation, message: "Function '\(functionDeclaration.identifier.name)' cannot have dynamic parameters", notes: notes)
}

static func ambiguousPayableValueParameter(_ functionDeclaration: FunctionDeclaration) -> Diagnostic {
return Diagnostic(severity: .error, sourceLocation: functionDeclaration.sourceLocation, message: "Ambiguous implicit payable value parameter. Only one parameter can be declared implicit with a currency type")
Expand Down
5 changes: 4 additions & 1 deletion Tests/SemanticTests/structs.flint
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ Foo :: (any) {
b(&b)
b(&self.b) // expected-error {{Function 'b' is not in scope or cannot be called using the caller capability 'any'}}
}
}

public func c(x: inout Test) {} // expected-error {{Function 'c' cannot have dynamic parameters}}
public func d(x: [Int]) {} // expected-error {{Function 'd' cannot have dynamic parameters}}
}

0 comments on commit 56cdb3f

Please sign in to comment.