Skip to content

Commit 504504b

Browse files
authored
Merge pull request #4980 from harlanhaskins/var-you-sure-about-that-swift-3.0-branch
Fix var-parameter detection for inferred params
2 parents 6a1b252 + 1bfee7b commit 504504b

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -746,21 +746,28 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,
746746
auto elementOptions = (options |
747747
(decl->isVariadic() ? TR_VariadicFunctionInput
748748
: TR_FunctionInput));
749-
bool hadError = TC.validateType(decl->getTypeLoc(), DC,
750-
elementOptions, resolver);
751-
749+
bool hadError = false;
750+
751+
// We might have a null typeLoc if this is a closure parameter list,
752+
// where parameters are allowed to elide their types.
753+
if (!decl->getTypeLoc().isNull()) {
754+
hadError |= TC.validateType(decl->getTypeLoc(), DC,
755+
elementOptions, resolver);
756+
}
757+
752758
Type Ty = decl->getTypeLoc().getType();
753-
if (decl->isVariadic() && !hadError) {
759+
if (decl->isVariadic() && !Ty.isNull() && !hadError) {
754760
Ty = TC.getArraySliceType(decl->getStartLoc(), Ty);
755761
if (Ty.isNull()) {
756762
hadError = true;
757763
}
758764
decl->getTypeLoc().setType(Ty);
759765
}
766+
760767
// If the param is not a 'let' and it is not an 'inout'.
761768
// It must be a 'var'. Provide helpful diagnostics like a shadow copy
762769
// in the function body to fix the 'var' attribute.
763-
if (!decl->isLet() && !decl->isInOut()) {
770+
if (!decl->isLet() && (Ty.isNull() || !Ty->is<InOutType>()) && !hadError) {
764771
auto func = dyn_cast_or_null<AbstractFunctionDecl>(DC);
765772
diagnoseAndMigrateVarParameterToBody(decl, func, TC);
766773
decl->setInvalid();
@@ -769,7 +776,7 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,
769776

770777
if (hadError)
771778
decl->getTypeLoc().setType(ErrorType::get(TC.Context), /*validated*/true);
772-
779+
773780
return hadError;
774781
}
775782

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

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

786792
auto type = param->getTypeLoc().getType();
787793
if (!type && param->hasType()) {

test/Sema/immutability.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ func invalid_inout(inout var x : Int) { // expected-error {{parameter may not ha
351351
func invalid_var(var x: Int) { // expected-error {{parameters may not have the 'var' specifier}}{{18-21=}} {{1-1= var x = x\n}}
352352

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

356358
func updateInt(_ x : inout Int) {}
357359

validation-test/IDE/crashers/069-swift-typechecker-typecheckparameterlist.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=A -source-filename=%s
2+
// REQUIRES: asserts
3+
func b(e:({#^A^#var e){

0 commit comments

Comments
 (0)