Skip to content

Assorted fixes to source ranges computed for the AST #4607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 3, 2016
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
3 changes: 3 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,9 @@ class PatternBindingEntry {

/// Override the initializer context.
void setInitContext(DeclContext *dc) { InitContext = dc; }

/// Retrieve the source range covered by this pattern binding.
SourceRange getSourceRange() const;
};

/// \brief This decl contains a pattern and optional initializer for a set
Expand Down
40 changes: 29 additions & 11 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,17 +918,32 @@ VarDecl *PatternBindingEntry::getAnchoringVarDecl() const {
return variables[0];
}

SourceRange PatternBindingEntry::getSourceRange() const {
SourceLoc endLoc;
getPattern()->forEachVariable([&](VarDecl *var) {
auto accessorsEndLoc = var->getBracesRange().End;
if (accessorsEndLoc.isValid()) endLoc = accessorsEndLoc;
});

// Check the initializer.
if (endLoc.isInvalid())
endLoc = getOrigInitRange().End;

// Check the pattern.
if (endLoc.isInvalid())
endLoc = getPattern()->getEndLoc();

SourceLoc startLoc = getPattern()->getStartLoc();
if (startLoc.isValid() != endLoc.isValid()) return SourceRange();

return SourceRange(startLoc, endLoc);
}

SourceRange PatternBindingDecl::getSourceRange() const {
SourceLoc startLoc = getStartLoc();

// Take the init of the last pattern in the list.
if (auto init = getPatternList().back().getInit()) {
SourceLoc EndLoc = init->getEndLoc();
if (EndLoc.isValid())
return { startLoc, EndLoc };
}
// If the last pattern had no init, we take the end of its pattern.
return { startLoc, getPatternList().back().getPattern()->getEndLoc() };
SourceLoc endLoc = getPatternList().back().getSourceRange().End;
if (startLoc.isValid() != endLoc.isValid()) return SourceRange();
return { startLoc, endLoc };
}

static StaticSpellingKind getCorrectStaticSpellingForDecl(const Decl *D) {
Expand Down Expand Up @@ -4568,9 +4583,12 @@ SourceRange FuncDecl::getSourceRange() const {
if (!B->isImplicit())
return { StartLoc, B->getEndLoc() };
}

if (this->isAccessor())
return StartLoc;

if (getBodyResultTypeLoc().hasLocation() &&
getBodyResultTypeLoc().getSourceRange().End.isValid() &&
!this->isAccessor())
getBodyResultTypeLoc().getSourceRange().End.isValid())
return { StartLoc, getBodyResultTypeLoc().getSourceRange().End };
auto LastParamListEndLoc = getParameterLists().back()->getSourceRange().End;
if (LastParamListEndLoc.isValid())
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ SourceRange TuplePattern::getSourceRange() const {
}

SourceRange TypedPattern::getSourceRange() const {
if (isImplicit()) {
if (isImplicit() || isPropagatedType()) {
// If a TypedPattern is implicit, then its type is definitely implicit, so
// we should ignore its location. On the other hand, the sub-pattern can
// be explicit or implicit.
Expand Down
4 changes: 2 additions & 2 deletions test/Parse/implicit_getter_incomplete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func test1() {

// Would trigger assertion when AST verifier checks source ranges ("child source range not contained within its parent")
func test2() { // expected-note {{match}}
var a : Int { // expected-note {{match}} expected-note {{did you mean 'a'?}}
var a : Int { // expected-note {{match}}
switch i { // expected-error {{unresolved identifier}}
} // expected-error {{'switch' statement body must have at least one}}
// expected-error 2 {{expected '}'}}
// expected-error@+1 2 {{expected '}'}}
8 changes: 4 additions & 4 deletions test/SourceKit/DocumentStructure/access_parse.swift.response
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@
key.setter_accessibility: source.lang.swift.accessibility.private,
key.name: "pubPrivSetProp",
key.offset: 1000,
key.length: 24,
key.length: 58,
key.typename: "Int",
key.nameoffset: 1004,
key.namelength: 14,
Expand All @@ -450,7 +450,7 @@
key.setter_accessibility: source.lang.swift.accessibility.fileprivate,
key.name: "pubFPSetProp",
key.offset: 1083,
key.length: 22,
key.length: 56,
key.typename: "Int",
key.nameoffset: 1087,
key.namelength: 12,
Expand All @@ -463,7 +463,7 @@
key.setter_accessibility: source.lang.swift.accessibility.internal,
key.name: "pubIntSetProp",
key.offset: 1161,
key.length: 23,
key.length: 57,
key.typename: "Int",
key.nameoffset: 1165,
key.namelength: 13,
Expand All @@ -475,7 +475,7 @@
key.accessibility: source.lang.swift.accessibility.public,
key.name: "pubGetOnly",
key.offset: 1227,
key.length: 20,
key.length: 44,
key.typename: "Int",
key.nameoffset: 1231,
key.namelength: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public var FooHelperUnnamedEnumeratorA2: Int { get }
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooHelperUnnamedEnumeratorA1",
key.offset: 123,
key.length: 37,
key.length: 45,
key.typename: "Int",
key.nameoffset: 127,
key.namelength: 28
Expand All @@ -208,7 +208,7 @@ public var FooHelperUnnamedEnumeratorA2: Int { get }
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooHelperUnnamedEnumeratorA2",
key.offset: 176,
key.length: 37,
key.length: 45,
key.typename: "Int",
key.nameoffset: 180,
key.namelength: 28
Expand Down
36 changes: 18 additions & 18 deletions test/SourceKit/InterfaceGen/gen_clang_module.swift.response
Original file line number Diff line number Diff line change
Expand Up @@ -3894,7 +3894,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooEnum1X",
key.offset: 380,
key.length: 23,
key.length: 31,
key.typename: "FooEnum1",
key.nameoffset: 384,
key.namelength: 9
Expand Down Expand Up @@ -3989,7 +3989,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooEnum2X",
key.offset: 582,
key.length: 23,
key.length: 31,
key.typename: "FooEnum2",
key.nameoffset: 586,
key.namelength: 9
Expand All @@ -4000,7 +4000,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooEnum2Y",
key.offset: 621,
key.length: 23,
key.length: 31,
key.typename: "FooEnum2",
key.nameoffset: 625,
key.namelength: 9
Expand Down Expand Up @@ -4095,7 +4095,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooEnum3X",
key.offset: 822,
key.length: 23,
key.length: 31,
key.typename: "FooEnum3",
key.nameoffset: 826,
key.namelength: 9
Expand All @@ -4106,7 +4106,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooEnum3Y",
key.offset: 861,
key.length: 23,
key.length: 31,
key.typename: "FooEnum3",
key.nameoffset: 865,
key.namelength: 9
Expand Down Expand Up @@ -4239,7 +4239,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "enableMince",
key.offset: 1258,
key.length: 41,
key.length: 49,
key.typename: "FooRuncingOptions",
key.nameoffset: 1269,
key.namelength: 11
Expand All @@ -4250,7 +4250,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "enableQuince",
key.offset: 1320,
key.length: 42,
key.length: 50,
key.typename: "FooRuncingOptions",
key.nameoffset: 1331,
key.namelength: 12
Expand Down Expand Up @@ -4750,7 +4750,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "fooProperty1",
key.offset: 3560,
key.length: 23,
key.length: 35,
key.typename: "Int32",
key.nameoffset: 3564,
key.namelength: 12,
Expand All @@ -4763,7 +4763,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "fooProperty2",
key.offset: 3608,
key.length: 23,
key.length: 35,
key.typename: "Int32",
key.nameoffset: 3612,
key.namelength: 12,
Expand All @@ -4775,7 +4775,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.accessibility: source.lang.swift.accessibility.public,
key.name: "fooProperty3",
key.offset: 3656,
key.length: 23,
key.length: 31,
key.typename: "Int32",
key.nameoffset: 3660,
key.namelength: 12,
Expand Down Expand Up @@ -4963,7 +4963,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.open,
key.name: "fooProperty3",
key.offset: 4226,
key.length: 23,
key.length: 31,
key.typename: "Int32",
key.nameoffset: 4230,
key.namelength: 12
Expand Down Expand Up @@ -5052,7 +5052,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_1",
key.offset: 4606,
key.length: 22,
key.length: 30,
key.typename: "Int32",
key.nameoffset: 4610,
key.namelength: 11
Expand All @@ -5063,7 +5063,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_2",
key.offset: 4644,
key.length: 22,
key.length: 30,
key.typename: "Int32",
key.nameoffset: 4648,
key.namelength: 11
Expand All @@ -5074,7 +5074,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_3",
key.offset: 4682,
key.length: 22,
key.length: 30,
key.typename: "Int32",
key.nameoffset: 4686,
key.namelength: 11
Expand All @@ -5085,7 +5085,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_4",
key.offset: 4759,
key.length: 23,
key.length: 31,
key.typename: "UInt32",
key.nameoffset: 4763,
key.namelength: 11
Expand All @@ -5096,7 +5096,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_5",
key.offset: 4798,
key.length: 23,
key.length: 31,
key.typename: "UInt64",
key.nameoffset: 4802,
key.namelength: 11
Expand All @@ -5107,7 +5107,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_REDEF_1",
key.offset: 4838,
key.length: 28,
key.length: 36,
key.typename: "Int32",
key.nameoffset: 4842,
key.namelength: 17
Expand All @@ -5118,7 +5118,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FOO_MACRO_REDEF_2",
key.offset: 4883,
key.length: 28,
key.length: 36,
key.typename: "Int32",
key.nameoffset: 4887,
key.namelength: 17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public var FooSubUnnamedEnumeratorA1: Int { get }
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooSubEnum1X",
key.offset: 248,
key.length: 29,
key.length: 37,
key.typename: "FooSubEnum1",
key.nameoffset: 252,
key.namelength: 12
Expand All @@ -431,7 +431,7 @@ public var FooSubUnnamedEnumeratorA1: Int { get }
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooSubEnum1Y",
key.offset: 293,
key.length: 29,
key.length: 37,
key.typename: "FooSubEnum1",
key.nameoffset: 297,
key.namelength: 12
Expand All @@ -442,7 +442,7 @@ public var FooSubUnnamedEnumeratorA1: Int { get }
key.setter_accessibility: source.lang.swift.accessibility.public,
key.name: "FooSubUnnamedEnumeratorA1",
key.offset: 339,
key.length: 34,
key.length: 42,
key.typename: "Int",
key.nameoffset: 343,
key.namelength: 25
Expand Down
8 changes: 4 additions & 4 deletions test/decl/var/static_var.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct InMemberFunc {
}
}

struct S { // expected-note 3{{extended type declared here}}
struct S { // expected-note 3{{extended type declared here}} expected-note{{did you mean 'S'?}}
static var v1: Int = 0
class var v2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}

Expand All @@ -116,7 +116,7 @@ extension S {
class let el2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
}

enum E { // expected-note 3{{extended type declared here}}
enum E { // expected-note 3{{extended type declared here}} expected-note{{did you mean 'E'?}}
static var v1: Int = 0
class var v2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}

Expand All @@ -141,7 +141,7 @@ extension E {
class let el2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
}

class C {
class C { // expected-note{{did you mean 'C'?}}
static var v1: Int = 0
class final var v3: Int = 0 // expected-error {{class stored properties not supported}}
class var v4: Int = 0 // expected-error {{class stored properties not supported}}
Expand Down Expand Up @@ -171,7 +171,7 @@ extension C {
static final let el4: Int = 0 // expected-error {{static declarations are already final}} {{10-16=}}
}

protocol P {
protocol P { // expected-note{{did you mean 'P'?}}
// Both `static` and `class` property requirements are equivalent in protocols rdar://problem/17198298
static var v1: Int { get }
class var v2: Int { get } // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
Expand Down