Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,21 +746,28 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,
auto elementOptions = (options |
(decl->isVariadic() ? TR_VariadicFunctionInput
: TR_FunctionInput));
bool hadError = TC.validateType(decl->getTypeLoc(), DC,
elementOptions, resolver);

bool hadError = false;

// We might have a null typeLoc if this is a closure parameter list,
// where parameters are allowed to elide their types.
if (!decl->getTypeLoc().isNull()) {
hadError |= TC.validateType(decl->getTypeLoc(), DC,
elementOptions, resolver);
}

Type Ty = decl->getTypeLoc().getType();
if (decl->isVariadic() && !hadError) {
if (decl->isVariadic() && !Ty.isNull() && !hadError) {
Ty = TC.getArraySliceType(decl->getStartLoc(), Ty);
if (Ty.isNull()) {
hadError = true;
}
decl->getTypeLoc().setType(Ty);
}

// If the param is not a 'let' and it is not an 'inout'.
// It must be a 'var'. Provide helpful diagnostics like a shadow copy
// in the function body to fix the 'var' attribute.
if (!decl->isLet() && !decl->isInOut()) {
if (!decl->isLet() && (Ty.isNull() || !Ty->is<InOutType>()) && !hadError) {
auto func = dyn_cast_or_null<AbstractFunctionDecl>(DC);
diagnoseAndMigrateVarParameterToBody(decl, func, TC);
decl->setInvalid();
Expand All @@ -769,7 +776,7 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,

if (hadError)
decl->getTypeLoc().setType(ErrorType::get(TC.Context), /*validated*/true);

return hadError;
}

Expand All @@ -780,8 +787,7 @@ bool TypeChecker::typeCheckParameterList(ParameterList *PL, DeclContext *DC,
bool hadError = false;

for (auto param : *PL) {
if (param->getTypeLoc().getTypeRepr())
hadError |= validateParameterType(param, DC, options, resolver, *this);
hadError |= validateParameterType(param, DC, options, resolver, *this);

auto type = param->getTypeLoc().getType();
if (!type && param->hasType()) {
Expand Down
4 changes: 3 additions & 1 deletion test/Sema/immutability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ func invalid_inout(inout var x : Int) { // expected-error {{parameter may not ha
func invalid_var(var x: Int) { // expected-error {{parameters may not have the 'var' specifier}}{{18-21=}} {{1-1= var x = x\n}}

}

func takesClosure(_: (Int) -> Int) {
takesClosure { (var d) in d } // expected-error {{parameters may not have the 'var' specifier}}
}

func updateInt(_ x : inout Int) {}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// RUN: %target-swift-ide-test -code-completion -code-completion-token=A -source-filename=%s
// REQUIRES: asserts
func b(e:({#^A^#var e){