Skip to content

Commit a118e24

Browse files
authored
Merge pull request #4679 from DougGregor/scope-map-fixes
2 parents 3e19a03 + eefea8f commit a118e24

File tree

6 files changed

+70
-35
lines changed

6 files changed

+70
-35
lines changed

lib/AST/ASTScope.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static bool hasAccessors(AbstractStorageDecl *asd) {
9090
case AbstractStorageDecl::ComputedWithMutableAddress:
9191
case AbstractStorageDecl::InheritedWithObservers:
9292
case AbstractStorageDecl::StoredWithObservers:
93-
return true;
93+
return asd->getBracesRange().isValid();
9494

9595
case AbstractStorageDecl::Stored:
9696
case AbstractStorageDecl::StoredWithTrivialAccessors:
@@ -1329,7 +1329,11 @@ SourceRange ASTScope::getSourceRangeImpl() const {
13291329
return SourceRange(charRange.getStart(), charRange.getEnd());
13301330
}
13311331

1332-
return SourceRange();
1332+
if (sourceFile.file->Decls.empty()) return SourceRange();
1333+
1334+
// Use the source ranges of the declarations in the file.
1335+
return SourceRange(sourceFile.file->Decls.front()->getStartLoc(),
1336+
sourceFile.file->Decls.back()->getEndLoc());
13331337

13341338
case ASTScopeKind::ExtensionGenericParams: {
13351339
// The generic parameters of an extension are available from the trailing
@@ -1750,7 +1754,8 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
17501754
break;
17511755

17521756
case ASTScopeKind::LocalDeclaration:
1753-
result.push_back(cast<ValueDecl>(localDeclaration));
1757+
if (auto value = dyn_cast<ValueDecl>(localDeclaration))
1758+
result.push_back(value);
17541759
break;
17551760

17561761
case ASTScopeKind::ConditionalClause:
@@ -1772,8 +1777,10 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
17721777
break;
17731778

17741779
case ASTScopeKind::ForStmtInitializer:
1775-
for (auto decl : forStmt->getInitializerVarDecls())
1776-
result.push_back(cast<ValueDecl>(decl));
1780+
for (auto decl : forStmt->getInitializerVarDecls()) {
1781+
if (auto value = dyn_cast<ValueDecl>(decl))
1782+
result.push_back(value);
1783+
}
17771784
break;
17781785

17791786
case ASTScopeKind::PatternInitializer:
@@ -1840,6 +1847,11 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
18401847
// Print the source location of the node.
18411848
auto printRange = [&]() {
18421849
auto range = getSourceRange();
1850+
if (range.isInvalid()) {
1851+
out << " [invalid source range]";
1852+
return;
1853+
}
1854+
18431855
auto startLineAndCol = sourceMgr.getLineAndColumn(range.Start);
18441856
auto endLineAndCol = sourceMgr.getLineAndColumn(range.End);
18451857

lib/AST/Decl.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,38 +3832,41 @@ ParamDecl *ParamDecl::createSelf(SourceLoc loc, DeclContext *DC,
38323832

38333833
/// Return the full source range of this parameter.
38343834
SourceRange ParamDecl::getSourceRange() const {
3835-
SourceRange range;
3836-
38373835
SourceLoc APINameLoc = getArgumentNameLoc();
38383836
SourceLoc nameLoc = getNameLoc();
3839-
3840-
if (APINameLoc.isValid() && nameLoc.isInvalid())
3841-
range = APINameLoc;
3842-
else if (APINameLoc.isInvalid() && nameLoc.isValid())
3843-
range = nameLoc;
3844-
else
3845-
range = SourceRange(APINameLoc, nameLoc);
3846-
3847-
if (range.isInvalid()) return range;
3848-
3837+
3838+
SourceLoc startLoc;
3839+
if (APINameLoc.isValid())
3840+
startLoc = APINameLoc;
3841+
else if (nameLoc.isValid())
3842+
startLoc = nameLoc;
3843+
else {
3844+
startLoc = getTypeLoc().getSourceRange().Start;
3845+
}
3846+
if (startLoc.isInvalid())
3847+
return SourceRange();
3848+
38493849
// It would be nice to extend the front of the range to show where inout is,
38503850
// but we don't have that location info. Extend the back of the range to the
38513851
// location of the default argument, or the typeloc if they are valid.
38523852
if (auto expr = getDefaultValue()) {
38533853
auto endLoc = expr->getEndLoc();
38543854
if (endLoc.isValid())
3855-
return SourceRange(range.Start, endLoc);
3855+
return SourceRange(startLoc, endLoc);
38563856
}
38573857

38583858
// If the typeloc has a valid location, use it to end the range.
38593859
if (auto typeRepr = getTypeLoc().getTypeRepr()) {
38603860
auto endLoc = typeRepr->getEndLoc();
38613861
if (endLoc.isValid() && !isTypeLocImplicit())
3862-
return SourceRange(range.Start, endLoc);
3862+
return SourceRange(startLoc, endLoc);
38633863
}
3864-
3865-
// Otherwise, just return the info we have about the parameter.
3866-
return range;
3864+
3865+
// The name has a location we can use.
3866+
if (nameLoc.isValid())
3867+
return SourceRange(startLoc, nameLoc);
3868+
3869+
return startLoc;
38673870
}
38683871

38693872
Type ParamDecl::getVarargBaseTy(Type VarArgT) {

lib/AST/NameLookup.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,12 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
598598

599599
// Dig out the type we're looking into.
600600
// FIXME: We shouldn't need to compute a type to perform this lookup.
601-
auto lookupType = dc->getDeclaredTypeInContext();
601+
Type lookupType;
602+
603+
if (dc->getAsProtocolOrProtocolExtensionContext())
604+
lookupType = dc->getSelfTypeInContext();
605+
else
606+
lookupType = dc->getDeclaredTypeInContext();
602607
if (!lookupType || lookupType->is<ErrorType>()) continue;
603608

604609
// If we're performing a static lookup, use the metatype.

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,15 +3421,6 @@ bool Parser::parseGetSetImpl(ParseDeclOptions Flags, ParameterList *Indices,
34213421
// an implicit fallthrough off the end.
34223422
if (Tok.is(tok::r_brace)) {
34233423
diagnose(Tok, diag::computed_property_no_accessors);
3424-
auto *TheDecl = createAccessorFunc(VarLBLoc, nullptr, TypeLoc(), nullptr,
3425-
SourceLoc(), Flags,
3426-
AccessorKind::IsGetter,
3427-
AddressorKind::NotAddressor, this,
3428-
SourceLoc());
3429-
3430-
TheDecl->setBody(BraceStmt::create(Context, VarLBLoc, {}, Tok.getLoc()));
3431-
TheDecl->setInvalid();
3432-
Decls.push_back(TheDecl);
34333424
return true;
34343425
}
34353426

lib/Parse/ParseStmt.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,11 +2017,10 @@ ParserResult<CatchStmt> Parser::parseStmtCatch() {
20172017
return makeParserCodeCompletionResult<CatchStmt>();
20182018
}
20192019

2020-
SourceLoc startOfBody = Tok.getLoc();
20212020
auto bodyResult = parseBraceItemList(diag::expected_lbrace_after_catch);
20222021
status |= bodyResult;
20232022
if (bodyResult.isNull()) {
2024-
bodyResult = makeParserErrorResult(BraceStmt::create(Context, startOfBody,
2023+
bodyResult = makeParserErrorResult(BraceStmt::create(Context, PreviousLoc,
20252024
{}, PreviousLoc,
20262025
/*implicit=*/ true));
20272026
}
@@ -2330,7 +2329,7 @@ ParserResult<Stmt> Parser::parseStmtForCStyle(SourceLoc ForLoc,
23302329
Status |= Body;
23312330
if (Body.isNull())
23322331
Body = makeParserResult(
2333-
Body, BraceStmt::create(Context, ForLoc, {}, PreviousLoc, true));
2332+
Body, BraceStmt::create(Context, PreviousLoc, {}, PreviousLoc, true));
23342333

23352334
return makeParserResult(
23362335
Status,

test/NameBinding/scope_map_lookup.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,28 @@ class LazyProperties {
5252

5353
lazy var prop5: Int = { self.value + 1 }()
5454
}
55+
56+
// Protocol extensions.
57+
// Extending via a superclass constraint.
58+
class Superclass {
59+
func foo() { }
60+
static func bar() { }
61+
62+
typealias Foo = Int
63+
}
64+
65+
protocol PConstrained4 { }
66+
67+
extension PConstrained4 where Self : Superclass {
68+
final func testFoo() -> Foo {
69+
foo()
70+
self.foo()
71+
72+
return Foo(5)
73+
}
74+
75+
final static func testBar() {
76+
bar()
77+
self.bar()
78+
}
79+
}

0 commit comments

Comments
 (0)