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

Const qualification comments #61492

Merged
merged 5 commits into from
Jun 11, 2019
Merged

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jun 3, 2019

I extracted some const-qualif knowledge from @eddyb. This is my attempt to turn that into comments.

Cc @oli-obk @eddyb

@rust-highfive
Copy link
Collaborator

r? @zackmdavis

(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 3, 2019
@zackmdavis
Copy link
Member

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned zackmdavis Jun 3, 2019
// Constant containing interior mutability (UnsafeCell).
/// Constant containing interior mutability (UnsafeCell).
/// This must be ruled out to make sure that evaluating the constant at compile-time
/// and run-time would produce the same result.
Copy link
Member

Choose a reason for hiding this comment

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

Nope, that's irrelevant. The problem is promoting e.g. &AtomicUsize and then mutating it.
That is, uses of the promoted references must not be able to distinguish between the promoted and unpromoted case (ignoring pointer addresses).

Copy link
Member Author

Choose a reason for hiding this comment

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

Well that's what I meant.^^

And also the part where any use of a const item must be equivalent to inlining its definition.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have expanded the description a bit, is this better?

src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
/// for value qualifications, and accumulates writes of
/// rvalue/call results to locals, in `local_qualif`.
/// For functions (constant or not), it also records
/// candidates for promotion in `promotion_candidates`.
Copy link
Member

Choose a reason for hiding this comment

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

Not just functions though, I think for everything? Promotion still has to do something in const/static, it's just slightly different than at runtime.

Copy link
Member Author

Choose a reason for hiding this comment

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

No idea, I just moved that comment up, I did not write it.^^

Copy link
Member Author

Choose a reason for hiding this comment

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

There's a comment in the file though saying

            // No need to do anything in constants and statics, as everything is "constant" anyway
            // so promotion would be useless.
            if self.mode != Mode::Static && self.mode != Mode::Const {

So it does seem to not do promotion the same way for statics and consts?

@@ -757,6 +788,9 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
// `let _: &'static _ = &(Cell::new(1), 2).1;`
let mut local_qualifs = self.qualifs_in_local(local);
local_qualifs[HasMutInterior] = false;
// Make sure there is no reason to prevent promotion.
// This is, in particular, the "implicit promotion" version of
// the check making sure that we don't run drop glue during const-eval.
Copy link
Member

Choose a reason for hiding this comment

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

This seems oddly specific - this is the original sole check, so comparing it to something newer seems off.
You could move the comment one line above and make it more about "any qualifications, except HasMutInterior (see above), disqualify from promotion".

Copy link
Member Author

@RalfJung RalfJung Jun 3, 2019

Choose a reason for hiding this comment

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

I don't think which check came first historically has any bearing here. Just because it grew that way doesn't mean that's the best way to explain it.

The other check is IMO clearly "more principled" by looking at the place where the side-effect actually occurs.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have adjusted the wording a bit.

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

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

Some proof-reading :)

src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Outdated Show resolved Hide resolved
@rust-highfive

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jun 5, 2019

☔ The latest upstream changes (presumably #61548) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Jun 9, 2019

☔ The latest upstream changes (presumably #61653) made this pull request unmergeable. Please resolve the merge conflicts.

@RalfJung
Copy link
Member Author

RalfJung commented Jun 9, 2019

This is bitrotting. @eddyb could you have a look at my edits?

@eddyb
Copy link
Member

eddyb commented Jun 10, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Jun 10, 2019

📌 Commit 38c7f3e has been approved by eddyb

@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 10, 2019
Centril added a commit to Centril/rust that referenced this pull request Jun 10, 2019
…ddyb

Const qualification comments

I extracted some const-qualif knowledge from @eddyb. This is my attempt to turn that into comments.

Cc @oli-obk 	@eddyb
Centril added a commit to Centril/rust that referenced this pull request Jun 10, 2019
…ddyb

Const qualification comments

I extracted some const-qualif knowledge from @eddyb. This is my attempt to turn that into comments.

Cc @oli-obk 	@eddyb
@bors
Copy link
Contributor

bors commented Jun 10, 2019

☔ The latest upstream changes (presumably #61506) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 10, 2019
@RalfJung
Copy link
Member Author

Rebased.

@bors r=eddyb

@bors
Copy link
Contributor

bors commented Jun 10, 2019

📌 Commit 0edf46f has been approved by eddyb

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 10, 2019
@bors
Copy link
Contributor

bors commented Jun 10, 2019

⌛ Testing commit 0edf46f with merge 0efa0a6a57bfe2ed3e05e02850f3c18d8747b767...

@bors
Copy link
Contributor

bors commented Jun 10, 2019

💥 Test timed out

@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 10, 2019
@RalfJung
Copy link
Member Author

@bors retry

@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 10, 2019
@bors
Copy link
Contributor

bors commented Jun 11, 2019

⌛ Testing commit 0edf46f with merge 9d9c7ad...

bors added a commit that referenced this pull request Jun 11, 2019
Const qualification comments

I extracted some const-qualif knowledge from @eddyb. This is my attempt to turn that into comments.

Cc @oli-obk 	@eddyb
@bors
Copy link
Contributor

bors commented Jun 11, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: eddyb
Pushing 9d9c7ad to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 11, 2019
@bors bors merged commit 0edf46f into rust-lang:master Jun 11, 2019
@RalfJung RalfJung deleted the const-qualif-comments branch June 21, 2019 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants