# Usr vs typeusr in sourcekit

**URL:** <https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759>\
**Category:** SourceKit-LSP\
**Created:** [August 9, 2019, 2:50pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759 "2019-08-09T14:50:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [August 9, 2019, 2:50pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/1 "2019-08-09T14:50:08Z")

</div>

I started looking into [[SR-10807] Implement textDocument/typeDefinition request · Issue #548 · apple/sourcekit-lsp · GitHub](https://bugs.swift.org/browse/SR-10807) and stumbled into a roadblock

```swift
struct Aaa { }
var bbb = Aaa()

```

If I log the dict received from sourcekit, the usr for `Aaa` is "s:3foo3AaaV", and typeusr for `bbb` is "$s3foo3AaaVD" which is nothing alike. It seems typeusr isn't the USR.

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [August 9, 2019, 3:42pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/2 "2019-08-09T15:42:58Z")

</div>

The usr of Aaa `s:3foo3AaaV` is a normal USR. This matches the index data.

The "typeusr" of bbb, `$s3foo3AaaVD` is a mangled symbol name.

It looks like the implementation of `printDeclTypeUSR` and `printTypeUSR` are not using the USR prefix. @Xi_Ge was this just an oversight? I see you implemented this back in 2016.

> **In CursorInfo response, include the mangle name of the type of the underlying decl.**
>
> ```swift
> commit a5e51a349436a8f009956b22278c55a0202fdc3f
> Author: Xi Ge <xi_ge@apple.com>
> Date: Tue Jul 12 12:01:37 2016
> 
> [SourceKit] In CursorInfo response, include the mangle name of the type of the underlying decl.
>     
> The mangled name of the type is identical to those for debugger. These
> mangled names allow us to reconstruct the type from AST and generate interface
> specifically for that type.
>     
> Related rdar://27306890
> 
> ```

The trailing "D" in the mangling of the type is a bit more mysterious to me. I can see it's added in [mangleDeclType](https://github.com/apple/swift/blob/b60f4ed499e3b2408ab6dfb614742351ba8e3040/lib/AST/ASTMangler.cpp#L388) and in [mangleTypeForDebugger](https://github.com/apple/swift/blob/b60f4ed499e3b2408ab6dfb614742351ba8e3040/lib/AST/ASTMangler.cpp#L372). @Xi_Ge and @Erik_Eckstein what's the point of the "D" here? Shouldn't we want this USR to match the decl USR?

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [August 9, 2019, 3:44pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/3 "2019-08-09T15:44:35Z")

</div>

Also, @Xi_Ge what do you think of the approach of using "typeusr" to find the definition of the type from the index in general? I guess this might not be sufficient for generic types?

---

<div class="post-metadata">

**Author:** ![Xi\_Ge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xi_ge/32/1594_2.png) [@Xi\_Ge](https://forums.swift.org/u/Xi_Ge)\
**Post date:** [August 9, 2019, 6:01pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/4 "2019-08-09T18:01:39Z")

</div>

`TypeUSR` as a concept is probably problematic since USRs are usually for indexing purposes and not all `TypeUSR` is indexable, such ad generic types as @blangmuir mentioned. I think we should rename the sourcekit entry for `TypeUSR` to `mangledTypeName` and the client needs to demangle that name to find the actual decl name they are looking for.

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [August 9, 2019, 6:08pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/5 "2019-08-09T18:08:38Z")

</div>

What do you think of adding a new optional field in cursor info that contains the USR of the type _decl_ if there is one? That should handle generic types, and for anything non-nominal, like a tuple or function, we can just say it's null.

```swift
var a =1 // a-> Int
var b = [1] // b -> Array
var c = (1, 2) // c -> nil
func test<T>(d: [T]) {} // d-> Array

```

---

<div class="post-metadata">

**Author:** ![Xi\_Ge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xi_ge/32/1594_2.png) [@Xi\_Ge](https://forums.swift.org/u/Xi_Ge)\
**Post date:** [August 9, 2019, 6:14pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/6 "2019-08-09T18:14:00Z")

</div>

I think that makes sense. This field should allow us to find the top-level type definition conveniently.

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [August 9, 2019, 8:23pm UTC](https://forums.swift.org/t/usr-vs-typeusr-in-sourcekit/27759/7 "2019-08-09T20:23:25Z")

</div>

I've updated [[SR-10807] Implement textDocument/typeDefinition request · Issue #548 · apple/sourcekit-lsp · GitHub](https://bugs.swift.org/browse/SR-10807) to describe this approach, thanks.
