Skip to content
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

scip: Generate symbols for local crates. #13456

Merged
merged 1 commit into from Nov 3, 2022

Conversation

emilio
Copy link
Contributor

@emilio emilio commented Oct 21, 2022

Consider something like:

// a.rs
pub struct Foo { .. } // Foo is "local 1"

fn something() {
    crate::b::Bar::new() // Bar is "local 1", but of "b.rs"
}

// b.rs
pub struct Bar { .. } // "local 1"

Without this there's no way to disambiguate whether "local 1" references "Bar" or "Foo".

@emilio
Copy link
Contributor Author

emilio commented Oct 21, 2022

@tjdevries @Veykril can you review? Thanks!

@tjdevries
Copy link
Contributor

@olafurpg / @varungandhi-src Is this generally how we deal w/ locals? I thought we restarted the local count for every file intentionally (particularly because of later goals of incremental compilation)

I think the actual problem must be that I missed some cases of when things should be exposed as non-local (i.e. a pub struct should not be a local, I think)

@emilio
Copy link
Contributor Author

emilio commented Oct 22, 2022

Right now ~everything defined in the current workspace uses a local symbol, I think. It makes sense to me off-hand, what should the symbol for Foo be in the test-case above otherwise? Happy to try to work on a better fix if given some pointers :)

@emilio
Copy link
Contributor Author

emilio commented Oct 22, 2022

(Given the rust compilation unit is the crate, not the file, it might be ~ok to do this regardless tho?)

@olafurpg
Copy link

Local symbols should only be used if the symbol cannot be referenced outside the file. In the case above, the pub struct Bar symbol should probably be global.

In scip-typescript, we emit global symbol for seemingly local variables. For example, see this test case where where we emit the definition of a global symbol for the key of a local object literal because the property a is part of the inferred type of the function

https://github.com/sourcegraph/scip-typescript/blob/main/snapshots/output/syntax/src/property-assignment.ts#L16

@emilio emilio changed the title scip: Allow disambiguating local symbols belonging to different files. scip: Allow disambiguating symbols belonging to different files. Oct 22, 2022
@emilio
Copy link
Contributor Author

emilio commented Oct 22, 2022

Ok, so I dug a bit and I think I found the root cause. The issue is that the moniker code was requiring a crate to have a repository in order to generate a moniker for it.

So if you're doing a scip index of a local crate which isn't published (which is our use case) then all symbols are incorrectly turned into local.

The fix I went for is to just allow the PackageInformation to not have a repo / version (which the lsif / scip formats allow afaict).

I added some tests to the scip code that fail without this patch.

@emilio emilio changed the title scip: Allow disambiguating symbols belonging to different files. scip: Generate symbols for local crates. Oct 22, 2022
repo?,
krate.version(db)?,
repo.unwrap_or_default(),
krate.version(db).unwrap_or_default(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to use unwrap_or_else(|| "file:///crate/path".into()) and unwrap_or_else(|| "v0.0.0".into()) or so.

Let me know if that's somehow preferable (though that would make the symbols for local crates non-deterministic, which would be somewhat unfortunate).

@olafurpg
Copy link

@emilio you can use a period character “.” for anonymous package names and versions. The only limitation from doing this is that you can’t support cross-repo navigation to your repo (only from your repo to external dependencies).

I can’t remember how well this is documented but scip-java uses this feature for almost all regular repositories (since cross-repo navigation works exclusively to packages where the name + version are easily accessible).

@emilio
Copy link
Contributor Author

emilio commented Oct 22, 2022

@emilio you can use a period character “.” for anonymous package names and versions. The only limitation from doing this is that you can’t support cross-repo navigation to your repo (only from your repo to external dependencies).

Cool, I've used the . placeholder for versionless crates, it's covered by the new tests. The code assumes all crates are named, and scip doesn't seem to use the repo url at the moment at least, so that should be all it's needed. Let me know if it looks good or more changes are needed :)

@emilio
Copy link
Contributor Author

emilio commented Nov 3, 2022

Gentle ping? Is there anything needed to get this reviewed? Thanks a lot in advance.

@Veykril
Copy link
Member

Veykril commented Nov 3, 2022

I am not familiar with scip, so if anyone of the sourcegraph team could drop another small review that would be appreciated

@tjdevries
Copy link
Contributor

One other option would be to allow passing the git hash of the project in for the current version -- not sure how much work that would be to do though.

Otherwise the PR looks good to me

@Veykril
Copy link
Member

Veykril commented Nov 3, 2022

Ye I don't think we can easily do that, I'll merge this then, thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Nov 3, 2022

📌 Commit 8039a07 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Nov 3, 2022

⌛ Testing commit 8039a07 with merge bbcb77e...

@bors
Copy link
Collaborator

bors commented Nov 3, 2022

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing bbcb77e to master...

@bors bors merged commit bbcb77e into rust-lang:master Nov 3, 2022
@emilio
Copy link
Contributor Author

emilio commented Nov 3, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants