Skip to content

Self not indexed in function signatures #65672

Open
@keith

Description

@keith

With this source:

struct Foo {
    static func bar() -> Self {
        .init()
    }
    static func baz() -> Foo {
        .init()
    }
}

-> Self is not indexed, where -> Foo is correctly indexed:

/*
       constructor=init()=s:3foo3FooVACycfc=definition, implicit, childOf
       struct=Foo=s:3foo3FooV=definition
       v  */
struct Foo {
    /*
                staticMethod=bar()=s:3foo3FooV3barACyFZ=definition, childOf
                v  */
    static func bar() -> Self {
        /*
         constructor=init()=s:3foo3FooVACycfc=reference, call, calledBy, containedBy
         v  */
        .init()
    }
    /*
                staticMethod=baz()=s:3foo3FooV3bazACyFZ=definition, childOf
                |        struct=Foo=s:3foo3FooV=reference, containedBy
                v        v  */
    static func baz() -> Foo {
        /*
         constructor=init()=s:3foo3FooVACycfc=reference, call, calledBy, containedBy
         v  */
        .init()
    }
}

Looking at the AST:

(source_file "self.swift"
  (struct_decl range=[self.swift:1:1 - line:8:1] "Foo" interface type='Foo.Type' access=internal non-resilient
    (func_decl range=[self.swift:2:5 - line:4:5] "bar()" interface type='(Foo.Type) -> () -> Foo' access=internal type
      (parameter "self")
      (parameter_list range=[self.swift:2:20 - line:2:21])
      (result
        (type_ident id='Self' bind=none))
      (call_expr type='Foo' location=self.swift:3:10 range=[self.swift:3:9 - line:3:15] nothrow
        (constructor_ref_call_expr type='() -> Foo' location=self.swift:3:10 range=[self.swift:3:9 - line:3:10] nothrow
          (declref_expr type='(Foo.Type) -> () -> Foo' location=self.swift:3:10 range=[self.swift:3:10 - line:3:10] decl=self.(file).Foo.init()@self.swift:1:8 function_ref=single)
          (argument_list implicit
            (argument
              (type_expr implicit type='Foo.Type' location=self.swift:3:9 range=[self.swift:3:9 - line:3:9] typerepr='Foo'))))
        (argument_list)))
    (func_decl range=[self.swift:5:5 - line:7:5] "baz()" interface type='(Foo.Type) -> () -> Foo' access=internal type
      (parameter "self")
      (parameter_list range=[self.swift:5:20 - line:5:21])
      (result
        (type_ident id='Foo' bind=self.(file)[email protected]:1:8))
      (call_expr type='Foo' location=self.swift:6:10 range=[self.swift:6:9 - line:6:15] nothrow
        (constructor_ref_call_expr type='() -> Foo' location=self.swift:6:10 range=[self.swift:6:9 - line:6:10] nothrow
          (declref_expr type='(Foo.Type) -> () -> Foo' location=self.swift:6:10 range=[self.swift:6:10 - line:6:10] decl=self.(file).Foo.init()@self.swift:1:8 function_ref=single)
          (argument_list implicit
            (argument
              (type_expr implicit type='Foo.Type' location=self.swift:6:9 range=[self.swift:6:9 - line:6:9] typerepr='Foo'))))
        (argument_list)))
    (constructor_decl implicit range=[self.swift:1:8 - line:1:8] "init()" interface type='(Foo.Type) -> () -> Foo' access=internal designated
      (parameter "self")
      (parameter_list)
      (brace_stmt implicit range=[self.swift:1:8 - line:1:8]
        (return_stmt range=[self.swift:1:8 - line:1:8])))))

This appears to be because Self is not bound to anything ((type_ident id='Self' bind=none)). Therefore it falls through this if statement and nothing is reported https://github.com/apple/swift/blob/9a57840c2e121c5335269a1cb555c303dd2b0d20/lib/IDE/SourceEntityWalker.cpp#L625

Repros at 9a57840 / Swift 5.8 (current release)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Self in struct & enumFeature → types: The 'Self' type in structures and enumerationsTypeResolverbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.declarationsFeature: declarationsfuncFeature → declarations: FunctionsindexingArea → source tooling: AST indexingmethodsFeature → functions: methods (member functions)source toolingArea: IDE support, SourceKit, and other source toolingswift 5.9type checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions