Skip to content

Commit

Permalink
Don't allow function types that differ in SIL representation to be ma…
Browse files Browse the repository at this point in the history
…tched by SameType constraint.

<rdar://problem/22181714> Crash when typing "signal"

Swift SVN r31594
  • Loading branch information
cwillmor committed Sep 1, 2015
1 parent 4ea7d1c commit 1c3b391
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,31 @@ static bool isFunctionTypeAcceptingNoArguments(Type type) {
return true;
}

// Returns 'false' (i.e. no error) if it is legal to match functions with the
// corresponding SIL function type representations and the given match kind.
static bool matchFunctionSILRepresentations(SILFunctionTypeRepresentation rep1,
SILFunctionTypeRepresentation rep2,
TypeMatchKind kind) {
switch (kind) {
case TypeMatchKind::BindType:
case TypeMatchKind::BindToPointerType:
case TypeMatchKind::SameType:
return rep1 != rep2;

case TypeMatchKind::ConformsTo:
llvm_unreachable("Not sure if we can end up here");

case TypeMatchKind::Subtype:
case TypeMatchKind::Conversion:
case TypeMatchKind::ExplicitConversion:
case TypeMatchKind::ArgumentConversion:
case TypeMatchKind::ArgumentTupleConversion:
case TypeMatchKind::OperatorArgumentTupleConversion:
case TypeMatchKind::OperatorArgumentConversion:
return false;
}
}

ConstraintSystem::SolutionKind
ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
TypeMatchKind kind, unsigned flags,
Expand Down Expand Up @@ -984,6 +1009,12 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
}
}

if (matchFunctionSILRepresentations(
func1->getExtInfo().getSILRepresentation(),
func2->getExtInfo().getSILRepresentation(), kind)) {
return SolutionKind::Error;
}

// Determine how we match up the input/result types.
TypeMatchKind subKind;
switch (kind) {
Expand Down
7 changes: 7 additions & 0 deletions test/Parse/c_function_pointers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ if true {
let k: @convention(c) () -> Int = S.staticMethod // expected-error{{}}
let m: @convention(c) () -> Int = C.staticMethod // expected-error{{}}
let n: @convention(c) () -> Int = C.classMethod // expected-error{{}}

// <rdar://problem/22181714> Crash when typing "signal"
let iuo_global: (() -> Int)! = global
let p: (@convention(c) () -> Int)! = iuo_global // expected-error{{a C function pointer can only be formed from a reference to a 'func' or a literal closure}}

func handler(callback: (@convention(c) () -> Int)!) {}
handler(iuo_global) // expected-error{{a C function pointer can only be formed from a reference to a 'func' or a literal closure}}
}

0 comments on commit 1c3b391

Please sign in to comment.