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

Make MatcherPos::stack a SmallVec. #55525

Merged
merged 1 commit into from
Nov 12, 2018

Conversation

nnethercote
Copy link
Contributor

This avoids some allocations.

This seems like a trivial change, but the compiler rejects it:

   Compiling syntax v0.0.0 (/home/njn/moz/rust1/src/libsyntax)                                      
error[E0597]: `initial` does not live long enough=========>           ] 89/110: syntax              
   --> libsyntax/ext/tt/macro_parser.rs:647:57
    |
647 |     let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
    |                                                         ^^^^^^^^^^^^ borrowed value does not live long enough
...
762 | }
    | -
    | |
    | `initial` dropped here while still borrowed
    | borrow later used here, when `initial` is dropped

error: aborting due to previous error

This is either a compiler bug, or there's some subtle thing I don't understand. The lifetimes sure seem straightforward: initial is declared, and then cur_items is declared immediately afterward, and it uses a reference to initial. The error message makes it sound like the compiler is dropping the variables in the wrong order.

r? @nikomatsakis, any idea what the problem is?

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 31, 2018
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-5.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:02616caa:start=1540954513588043569,finish=1540954573797698244,duration=60209654675
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#Pull-Requests-and-Security-Restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
Setting environment variables from .travis.yml
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
---
151412 ./src/tools/clang
149644 ./obj/build/bootstrap/debug/incremental
149116 ./src/llvm-emscripten/test
134188 ./obj/build/bootstrap/debug/incremental/bootstrap-32pr67l4sa8g0
134184 ./obj/build/bootstrap/debug/incremental/bootstrap-32pr67l4sa8g0/s-f680ttfgmx-7mx2uo-ycmuivyxm2px
107668 ./obj/build/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends
104700 ./src/tools/lldb
93748 ./src/tools/clang/test
77476 ./obj/build/x86_64-unknown-linux-gnu/stage0-rustc

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@nikomatsakis
Copy link
Contributor

@nnethercote I'm a bit confused, where is that diff ? is that in the PR somewhere/

@nnethercote
Copy link
Contributor Author

@nikomatsakis: this PR contains the diff that triggers the compile error.

@nnethercote
Copy link
Contributor Author

So if you apply this PR's diff to your own tree and compile, you'll get the error that I suspect is bogus.

@nikomatsakis
Copy link
Contributor

I see

@nikomatsakis
Copy link
Contributor

(Trying this now locally)

@nikomatsakis
Copy link
Contributor

Hmm, so, I don't think that the borrow checker is wrong but the error reporting is ungreat here (and this isn't my favorite part of the language). I guess SmallVec is defined in the smallvec crate, right? I think it lacks the #[may_dangle] annotations which tell us that the T will not be used by the destructor (except to be dropped) -- Vec has such annotations (they are not permitted in stable code, though).

This forces us to be more conservative with SmallVec than Vec.

This is combined with the fact that the MatcherPosHandle definition has too few lifetime parameters:

enum MatcherPosHandle<'a> {
    Ref(&'a mut MatcherPos<'a>),
    Box(Box<MatcherPos<'a>>),
}

Here, the single parameter 'a is used after an &mut, which requires a strict equality and doesn't permit "lifetime approximation" (the lifetime can't get any smaller).

So what winds up happening is that .. somehow .. the lifetiime of the borrow winds up extended. Anyway, sorry this explanation isn't that coherent =) I'll dig a bit more.

@nikomatsakis
Copy link
Contributor

OK, pushed a fix I believe.

@rust-highfive

This comment has been minimized.

@nikomatsakis nikomatsakis force-pushed the MatcherPos-stack-SmallVec branch from fb1c9eb to f88f3e3 Compare November 2, 2018 20:27
@nikomatsakis
Copy link
Contributor

(Warning: force pushed over my commit to resolve tidy failure)

@nnethercote
Copy link
Contributor Author

Goodness! Thank you for figuring that out, I never would have.

@nnethercote
Copy link
Contributor Author

@nikomatsakis: is it reasonable to push this as-is? Or should I squash the two patches together?

This avoids some allocations.
@nnethercote nnethercote force-pushed the MatcherPos-stack-SmallVec branch from f88f3e3 to 68e76dc Compare November 12, 2018 00:38
@nnethercote
Copy link
Contributor Author

@nikomatsakis: I merged the patches and put you as the author.

@nnethercote
Copy link
Contributor Author

@bors r+

@bors
Copy link
Contributor

bors commented Nov 12, 2018

📌 Commit 68e76dc has been approved by nnethercote

@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 Nov 12, 2018
@bors
Copy link
Contributor

bors commented Nov 12, 2018

⌛ Testing commit 68e76dc with merge d8f4c9f...

bors added a commit that referenced this pull request Nov 12, 2018
…ercote

Make `MatcherPos::stack` a `SmallVec`.

This avoids some allocations.

This seems like a trivial change, but the compiler rejects it:
```
   Compiling syntax v0.0.0 (/home/njn/moz/rust1/src/libsyntax)
error[E0597]: `initial` does not live long enough=========>           ] 89/110: syntax
   --> libsyntax/ext/tt/macro_parser.rs:647:57
    |
647 |     let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
    |                                                         ^^^^^^^^^^^^ borrowed value does not live long enough
...
762 | }
    | -
    | |
    | `initial` dropped here while still borrowed
    | borrow later used here, when `initial` is dropped

error: aborting due to previous error
```
This is either a compiler bug, or there's some subtle thing I don't understand. The lifetimes sure seem straightforward: `initial` is declared, and then `cur_items` is declared immediately afterward, and it uses a reference to `initial`. The error message makes it sound like the compiler is dropping the variables in the wrong order.

r? @nikomatsakis, any idea what the problem is?
@bors
Copy link
Contributor

bors commented Nov 12, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nnethercote
Pushing d8f4c9f to master...

@bors bors merged commit 68e76dc into rust-lang:master Nov 12, 2018
@nnethercote nnethercote deleted the MatcherPos-stack-SmallVec branch November 12, 2018 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants