Skip to content

Commit 49d134f

Browse files
authored
[Parse] Stop lying about QuestionLoc of implicit OptionalTypeRepr (#7384)
In conditional statement let/var patterns. Set invalid location instead.
1 parent 1b3d29a commit 49d134f

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,12 @@ class OptionalTypeRepr : public TypeRepr {
526526

527527
private:
528528
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
529-
SourceLoc getEndLocImpl() const { return QuestionLoc; }
530-
SourceLoc getLocImpl() const { return QuestionLoc; }
529+
SourceLoc getEndLocImpl() const {
530+
return QuestionLoc.isValid() ? QuestionLoc : Base->getEndLoc();
531+
}
532+
SourceLoc getLocImpl() const {
533+
return QuestionLoc.isValid() ? QuestionLoc : Base->getLoc();
534+
}
531535
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
532536
friend class TypeRepr;
533537
};

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ parseOptionalPatternTypeAnnotation(ParserResult<Pattern> result,
948948
// In an if-let, the actual type of the expression is Optional of whatever
949949
// was written.
950950
if (isOptional)
951-
repr = new (Context) OptionalTypeRepr(repr, Tok.isNot(tok::eof) ? Tok.getLoc() : PreviousLoc);
951+
repr = new (Context) OptionalTypeRepr(repr, SourceLoc());
952952

953953
return makeParserResult(new (Context) TypedPattern(P, repr));
954954
}

test/Parse/recovery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ func testMultiPatternConditionRecovery(x: Int?) {
638638

639639
// <rdar://problem/20883210> QoI: Following a "let" condition with boolean condition spouts nonsensical errors
640640
guard let x: Int? = 1, x == 1 else { }
641-
// expected-warning @-1 {{explicitly specified type 'Int?' adds an additional level of optional to the initializer, making the optional check always succeed}} {{16-22=Int}}
641+
// expected-warning @-1 {{explicitly specified type 'Int?' adds an additional level of optional to the initializer, making the optional check always succeed}} {{16-20=Int}}
642642
}
643643

644644
// rdar://20866942

test/stmt/if_while_var.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ if 1 != 2, let x = opt,
7979

8080
// <rdar://problem/20457938> typed pattern is not allowed on if/let condition
8181
if 1 != 2, let x : Int? = opt {} // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
82-
// expected-warning @-1 {{explicitly specified type 'Int?' adds an additional level of optional to the initializer, making the optional check always succeed}} {{20-26=Int}}
82+
// expected-warning @-1 {{explicitly specified type 'Int?' adds an additional level of optional to the initializer, making the optional check always succeed}} {{20-24=Int}}
8383

8484
if 1 != 2, case let x? : Int? = 42 {} // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
8585
// expected-warning @-1 {{non-optional expression of type 'Int' used in a check for optionals}}

validation-test/compiler_crashers/28630-anonymous-namespace-verifier-walktostmtpost-swift-stmt.swift renamed to validation-test/compiler_crashers_fixed/28630-anonymous-namespace-verifier-walktostmtpost-swift-stmt.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
[{{#if0 guard let c:}

0 commit comments

Comments
 (0)