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

Disambiguate between SourceFiles from different crates even if they have the same path #86368

Merged
merged 3 commits into from
Jun 22, 2021

Conversation

michaelwoerister
Copy link
Member

This PR fixes an ICE that can occur when the compiler encounters a source file that is part of both the local crate and an upstream crate:

  1. While importing source files from an upstream crate the compiler creates a SourceFile entry for foo.rs in the SourceMap. Since this is an imported source file its src field is None.
  2. At a later point the parser encounters foo.rs again. It tells the SourceMap to load the file but because we already have an entry for foo.rs the SourceMap will return the existing version with src == None.
  3. The parser proceeds under the assumption that src.is_some() and panics when actually trying to use the file's contents.

This PR fixes the issue by adding the source file's associated CrateNum to the SourceMap's interning key. As a consequence the two instances of the file will each have a separate entry in the SourceMap. They just happen to share the same file path. This approach seemed less problematic to me than trying to mutate the SourceFile after it had already been created.

Another, more involved, approach might be to merge the src and the external_src field.

Fixes #85955

@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 16, 2021
@michaelwoerister michaelwoerister added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 16, 2021
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 16, 2021

📌 Commit 2942d18145b8f5e878da7f3adc33ff370f320624 has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2021
@michaelwoerister
Copy link
Member Author

Thanks for the review, @davidtwco!

@wesleywiser
Copy link
Member

Since this issue was introduced in #83813 which landed in 1.54, I'm nominating for backport to beta (1.54).

@wesleywiser wesleywiser added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jun 17, 2021
@gilescope
Copy link
Contributor

I've built this branch and confirm this solves the ICE for me. Thank you so much @michaelwoerister !

@bors
Copy link
Contributor

bors commented Jun 19, 2021

⌛ Testing commit 2942d18145b8f5e878da7f3adc33ff370f320624 with merge edadf7cd25a7bbac01eb7438bf7b913cab443d65...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jun 19, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 19, 2021
@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 21, 2021
@davidtwco
Copy link
Member

Looks like a legitimate failure.

@michaelwoerister
Copy link
Member Author

I'll look into it.
@bors rollup=never

@michaelwoerister
Copy link
Member Author

michaelwoerister commented Jun 21, 2021

It looks like #85834 might be the culprit: CrateNum now gets encoded as a StableCrateId but the decoder in question still decodes it as a u32.

@rust-log-analyzer

This comment has been minimized.

@michaelwoerister
Copy link
Member Author

I fixed the issue by encoding the CrateNum part as a StableCrateId and then mapping that back to CrateNum on-demand. I also tried to remove the default encoding and decoding impls for CrateNum in order to make it more difficult to run into an encoder/decoder mismatch like this in the future but it turns out the default impls are still used somewhere. I opened #86540 regarding that.

Do you want to take another look, @davidtwco?

@michaelwoerister michaelwoerister added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 22, 2021
@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 22, 2021

📌 Commit c3c4ab5 has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 22, 2021
@bors
Copy link
Contributor

bors commented Jun 22, 2021

⌛ Testing commit c3c4ab5 with merge 80926fc...

@bors
Copy link
Contributor

bors commented Jun 22, 2021

☀️ Test successful - checks-actions
Approved by: davidtwco
Pushing 80926fc to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 22, 2021
@bors bors merged commit 80926fc into rust-lang:master Jun 22, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 22, 2021
@gilescope
Copy link
Contributor

Thanks again @michaelwoerister for fixing this - much obliged!

@michaelwoerister
Copy link
Member Author

You're welcome, @gilescope!

@apiraino
Copy link
Contributor

Beta backport accepted as per compiler team on Zulip

@rustbot label +beta-accepted

@apiraino apiraino added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jun 24, 2021
@pnkfelix
Copy link
Member

This seems to have caused a mild (approx. -1%) regression on ctfe-stress-4 for the incr-unchanged case.

My intuition is that it may just be an unavoidable cost of the change in representation that was introduced here.

(Should a perf run have been done on this PR before it landed? Not an easy call to make; only one benchmark significantly regressed, but from the code itself here, this was a key data structure being modified, so maybe a perf run is always warranted in such cases.)

@cuviper
Copy link
Member

cuviper commented Jul 2, 2021

@michaelwoerister I found this non-trivial to backport to beta, particularly with errors in on_disk_cache.rs. Would you be able to prepare that backport yourself?

@michaelwoerister
Copy link
Member Author

Sorry, @cuviper, I totally overlooked your message. A backport PR is up at #87393.

@Mark-Simulacrum Mark-Simulacrum modified the milestones: 1.55.0, 1.54.0 Jul 23, 2021
@Mark-Simulacrum Mark-Simulacrum removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jul 23, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 23, 2021
…6368, r=Mark-Simulacrum

Backport rust-lang#85834 and rust-lang#86368

As per discussion in https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/backport.20.2386368, backporting rust-lang#85834 too seems like the safest option for cleanly backporting rust-lang#86368.

r? `@Mark-Simulacrum`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: "cannot lex source_file without source" with integration test