Skip to content

Commit 955ac3b

Browse files
authored
Merge pull request #17866 from DougGregor/forward-reference-local-type
2 parents 88fb124 + 62d13e5 commit 955ac3b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,14 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
601601
auto isValid = [&](ValueDecl *D) {
602602
// FIXME: The source-location checks won't make sense once
603603
// EnableASTScopeLookup is the default.
604+
//
605+
// Note that we allow forward references to types, because they cannot
606+
// capture.
604607
if (Loc.isValid() && D->getLoc().isValid() &&
605608
D->getDeclContext()->isLocalContext() &&
606609
D->getDeclContext() == DC &&
607-
Context.SourceMgr.isBeforeInBuffer(Loc, D->getLoc())) {
610+
Context.SourceMgr.isBeforeInBuffer(Loc, D->getLoc()) &&
611+
!isa<TypeDecl>(D)) {
608612
localDeclAfterUse = D;
609613
return false;
610614
}

test/Compatibility/local_types.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
func foo() {
4+
// Okay to reference a type declared later in the same function.
5+
_ = Visitor()
6+
struct Visitor { }
7+
}

0 commit comments

Comments
 (0)