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

Allow constants to refer statics #66302

Closed

Conversation

spastorino
Copy link
Member

r? @oli-obk, now @eddyb will be happy ❤️

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

The job x86_64-gnu-llvm-6.0 of your PR failed (pretty log, 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.
2019-11-11T17:34:25.4383043Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-11-11T17:34:25.4564046Z ##[command]git config gc.auto 0
2019-11-11T17:34:25.4640502Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-11-11T17:34:25.4706572Z ##[command]git config --get-all http.proxy
2019-11-11T17:34:25.4857157Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/66302/merge:refs/remotes/pull/66302/merge
---
2019-11-11T18:34:51.1314963Z .................................................................................................... 1400/9226
2019-11-11T18:34:57.6132852Z .................................................................................................... 1500/9226
2019-11-11T18:35:03.4621464Z .................................................................................................... 1600/9226
2019-11-11T18:35:12.7528212Z .................................................................................................... 1700/9226
2019-11-11T18:35:20.9485537Z .i.................................................................................................. 1800/9226
2019-11-11T18:35:27.7532786Z .....................................................................................iiiii.......... 1900/9226
2019-11-11T18:35:48.9628203Z .................................................................................................... 2100/9226
2019-11-11T18:35:51.2329697Z .................................................................................................... 2200/9226
2019-11-11T18:35:53.7347787Z .................................................................................................... 2300/9226
2019-11-11T18:36:04.5315609Z .................................................................................................... 2400/9226
---
2019-11-11T18:38:59.1828601Z ................................................................................i...............i... 4700/9226
2019-11-11T18:39:06.7204196Z .................................................................................................... 4800/9226
2019-11-11T18:39:15.8858904Z .................................................................................................... 4900/9226
2019-11-11T18:39:20.9763665Z .................................................................................................... 5000/9226
2019-11-11T18:39:33.0290909Z ...................................................................................ii.ii...........i 5100/9226
2019-11-11T18:39:41.8491431Z ..................i................................................................................. 5300/9226
2019-11-11T18:39:51.6745105Z .................................................................................................... 5400/9226
2019-11-11T18:39:58.6240559Z .................................................................i.................................. 5500/9226
2019-11-11T18:40:05.7524431Z .................................................................................................... 5600/9226
2019-11-11T18:40:05.7524431Z .................................................................................................... 5600/9226
2019-11-11T18:40:14.0055535Z .................................................................................................... 5700/9226
2019-11-11T18:40:22.7389031Z ..................................................ii...i..ii...........i............................ 5800/9226
2019-11-11T18:40:46.0612034Z .................................................................................................... 6000/9226
2019-11-11T18:40:52.1638121Z .................................................................................................... 6100/9226
2019-11-11T18:40:52.1638121Z .................................................................................................... 6100/9226
2019-11-11T18:40:57.2881850Z .....................................................................i..ii.......................... 6200/9226
2019-11-11T18:41:26.3876383Z .................................................................................................... 6400/9226
2019-11-11T18:41:28.5391076Z .....................................i.............................................................. 6500/9226
2019-11-11T18:41:30.8971008Z .................................................................................................... 6600/9226
2019-11-11T18:41:33.3620701Z .....................i.............................................................................. 6700/9226
---
2019-11-11T18:46:49.4045921Z  finished in 5.879
2019-11-11T18:46:49.4240076Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T18:46:49.5984549Z 
2019-11-11T18:46:49.5984839Z running 156 tests
2019-11-11T18:46:52.5653505Z iiii....iii......iii..iiii...i.............................i..i..................i....i...........ii 100/156
2019-11-11T18:46:54.3276231Z .i.i..iiii..............i.........iii.i.........ii......
2019-11-11T18:46:54.3277991Z 
2019-11-11T18:46:54.3284667Z  finished in 4.904
2019-11-11T18:46:54.3473189Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T18:46:54.5055532Z 
---
2019-11-11T18:46:56.5715052Z  finished in 2.044
2019-11-11T18:46:56.5715432Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T18:46:56.5715989Z 
2019-11-11T18:46:56.5716077Z running 9 tests
2019-11-11T18:46:56.5716447Z iiiiiiiii
2019-11-11T18:46:56.5716783Z 
2019-11-11T18:46:56.5716826Z  finished in 0.162
2019-11-11T18:46:56.5889791Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T18:46:56.7523665Z 
---
2019-11-11T18:47:16.5709549Z  finished in 19.254
2019-11-11T18:47:16.5709884Z Check compiletest suite=debuginfo mode=debuginfo-gdb+lldb (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T18:47:16.5709925Z 
2019-11-11T18:47:16.5709968Z running 123 tests
2019-11-11T18:47:39.9232538Z .iiiii...i.....i..i...i..i.i.i..i.ii..i.i.....i..i....ii..........iiii..........i...ii...i.......ii. 100/123
2019-11-11T18:47:44.5042773Z i.i.i......iii.i.....ii
2019-11-11T18:47:44.5047208Z 
2019-11-11T18:47:44.5047621Z  finished in 28.642
2019-11-11T18:47:44.5053870Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T18:47:44.5054778Z Copying stage2 rustc from stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
---
2019-11-11T18:57:25.2699131Z      Running build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/release/deps/collectionstests-36cc9905d18c7d2a
2019-11-11T18:57:25.2736296Z 
2019-11-11T18:57:25.2736431Z running 602 tests
2019-11-11T18:57:25.3149691Z .................................................................................................... 100/602
2019-11-11T18:57:25.3219363Z ...........................................thread '<unnamed>' panicked at 'explicit panic.', src/liballoc/../liballoc/tests/slice.rs:1429:17
2019-11-11T18:57:26.0177282Z .................................................................................................... 300/602
2019-11-11T18:57:26.0721838Z .................................................................................................... 400/602
2019-11-11T18:57:26.1035376Z .................................................................................................... 500/602
2019-11-11T18:57:26.1695103Z .................................................................................................... 600/602
---
2019-11-11T18:59:35.0465266Z 
2019-11-11T18:59:35.0465955Z    Doc-tests core
2019-11-11T18:59:40.0205812Z 
2019-11-11T18:59:40.0206045Z running 2418 tests
2019-11-11T18:59:50.2050255Z ......iiiii......................................................................................... 100/2418
2019-11-11T19:00:00.4880467Z ................................................................................ii.................. 200/2418
2019-11-11T19:00:24.5417735Z ..i................................................................................................. 400/2418
2019-11-11T19:00:24.5417735Z ..i................................................................................................. 400/2418
2019-11-11T19:00:34.7217948Z ..................................................i..i.................iiii......................... 500/2418
2019-11-11T19:00:53.6384579Z .................................................................................................... 700/2418
2019-11-11T19:01:03.2743824Z .................................................................................................... 800/2418
2019-11-11T19:01:13.1327368Z .................................................................................................... 900/2418
2019-11-11T19:01:22.8545945Z .................................................................................................... 1000/2418
---
2019-11-11T19:05:20.0425875Z 
2019-11-11T19:05:20.0426103Z running 1000 tests
2019-11-11T19:05:40.0701128Z i................................................................................................... 100/1000
2019-11-11T19:05:50.8418781Z .................................................................................................... 200/1000
2019-11-11T19:05:58.5843881Z ...................iii......i......i...i......i..................................................... 300/1000
2019-11-11T19:06:03.6859454Z .................................................................................................... 400/1000
2019-11-11T19:06:10.8472399Z ...........................................i..i.................................ii.................. 500/1000
2019-11-11T19:06:24.4126853Z .................................................................................................... 700/1000
2019-11-11T19:06:24.4126853Z .................................................................................................... 700/1000
2019-11-11T19:06:31.6101683Z ..........................iiii...................................................................... 800/1000
2019-11-11T19:06:46.6498870Z .................................................................................................... 900/1000
2019-11-11T19:06:54.0233364Z ................................................iiii................................................ 1000/1000
2019-11-11T19:06:54.0236687Z test result: ok. 980 passed; 0 failed; 20 ignored; 0 measured; 0 filtered out
2019-11-11T19:06:54.0236758Z 
2019-11-11T19:06:54.0272276Z  finished in 185.150
2019-11-11T19:06:54.0287116Z Testing term stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2019-11-11T19:24:28.9158611Z   local time: Mon Nov 11 19:24:28 UTC 2019
2019-11-11T19:24:28.9915974Z   network time: Mon, 11 Nov 2019 19:24:28 GMT
2019-11-11T19:24:28.9921675Z == end clock drift check ==
2019-11-11T19:24:30.6097960Z 
2019-11-11T19:24:30.6188349Z ##[error]Bash exited with code '1'.
2019-11-11T19:24:30.6247270Z ##[section]Starting: Checkout
2019-11-11T19:24:30.6248733Z ==============================================================================
2019-11-11T19:24:30.6248792Z Task         : Get sources
2019-11-11T19:24:30.6248831Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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)

@Centril Centril added needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. relnotes Marks issues that should be documented in the release notes of the next release. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Nov 12, 2019
@Centril
Copy link
Contributor

Centril commented Nov 12, 2019

I'm not sure why we are doing this change. If we are to do so, I think an RFC would be in order. For the time being, we have an RFC that explicitly states that this should not be allowed.

Also, do we have any test which forbids the following?

extern {
    static FOO: u8;
}

const X: u8 = unsafe { FOO };

@spastorino spastorino force-pushed the constants-can-refer-statics branch from 723630e to 774445d Compare November 12, 2019 11:13
@spastorino
Copy link
Member Author

@Centril I've just added the test you mentioned, good point ;).

@Centril
Copy link
Contributor

Centril commented Nov 12, 2019

Thanks!

Since the main purpose of this work is to simplify compiler internals rather than as a new language feature, I would suggest moving the existing check to HIR. That shouldn't be too difficult I think -- could be its own pass or we could do it in typeck maybe.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 12, 2019

I'm not sure why we are doing this change. If we are to do so, I think an RFC would be in order.

For the time being, we have an RFC that explicitly states that this should not be allowed.

We have? I was under the assumption this is just a restriction left over from the old const evaluator. The miri-based const eval can handle this without any problems. Which RFC are you referring to?

@oli-obk
Copy link
Contributor

oli-obk commented Nov 12, 2019

@Centril and I discussed this a bit via chat. Here's the summary:

  1. This does not introduce any new runtime behaviour, because one could always have used a static item instead of a const item and it would have worked just as well
  2. This does allow array lengths, enum discriminants and const items to take references to statics and even dereference them to get a value out.
  3. While const FOO: &AtomicUsize = &AtomicUsize::new(42); is illegal because the atomic may be arbitrarily duplicated or deduplicated, const FOO: &AtomicUsize = &SOME_STATIC; is legal, because the constant referes always to the same named static item's memory
    • this extends even to lazy or extern statics, while you can make a constant refer to a them, you can't actually read from them because the const evaluator catches that at runtime.
  4. The only problematic situation that exists is const fn. Right now const fn cannot refer to static items, because that would allow one to write a const fn like
    const fn foo() -> usize {
        static FOO: AtomicUsize = AtomicUsize::new(0);
        FOO.fetch_add(1, Ordering::Relaxed)
    }
    (assuming powerful enough const eval features).
    • Even if we forbade statics, we could use the const BAR: &AtomicUsize = &FOO; indirection to work around the static check.
    • While we could just forbid constants in const fn that refer to statics anywhere, that won't work for associated constants like
      const fn foo<T: Trait>() -> usize {
          T::ASSOC_CONST.fetch_add(1, Ordering::Relaxed)
      }
    • We could still do the check at monomorphization time, but that would mean that a use of a function would fail to compile depending on the type the user passed for the generic parameter

cc @RalfJung

@bors
Copy link
Contributor

bors commented Nov 14, 2019

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

@RalfJung
Copy link
Member

RalfJung commented Nov 16, 2019

The crucial thing to make sure is that CTFE does not read from (let alone write do) mutable statics. We should make sure to have a few miri-unleash tests that cover this. Cnetril mentioned one, @oli-obk mentioned this one:

const FOO: usize {
    static FOO: AtomicUsize = AtomicUsize::new(0);
    FOO.fetch_add(1, Ordering::Relaxed)
};

and then there also is

const FOO: usize {
    static FOO: AtomicUsize = AtomicUsize::new(0);
    *(&FOO as *const _ as *const usize)
};

and

static mut MUTABLE: u32 = 0;
const BAD: u32 = unsafe { MUTABLE };

All of these should fail even with miri-unleashed, to make sure that our dynamic checks are covering all possibilities.

What remains then is the question of whether we want to have static checks on top of the dynamic ones, and what those are good for. Doing only dynamic checks leads to monomorphization-time errors. But from how I understood @oli-obk, we either already have those or ruling those out also rules out way too many useful things, and hence monomorphization-time errors are the better option? I do not have a strong stanza on this, if @oli-obk and @Centril agree on that that's good enough for me. I just want to see the Miri engine do enough checks to rule out all these examples. :) (And I think so far it does not do enough checks, actually.)

@JohnCSimon JohnCSimon 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 Nov 24, 2019
@JohnCSimon
Copy link
Member

Ping from triage
@spastorino Can you please address the merge conflicts?
Thank you!

@spastorino
Copy link
Member Author

@JohnCSimon, I don't think it makes sense to rebase this until we don't figure out a way to make this idea work.

@spastorino spastorino added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 24, 2019
@Centril
Copy link
Contributor

Centril commented Dec 16, 2019

But from how I understood @oli-obk, we either already have those or ruling those out also rules out way too many useful things, and hence monomorphization-time errors are the better option? I do not have a strong stanza on this, if @oli-obk and @Centril agree on that that's good enough for me.

My stance is that should not be adding post-monomorphization errors and that const fns should remain deterministic when executed at run-time.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 16, 2019

I'm currently unable to come up with a non-postmonomorphization plan that would not end up making const qualif more complex or require new marker traits

Let's close until we have a plan for working this out

@oli-obk oli-obk closed this Dec 16, 2019
@RalfJung
Copy link
Member

CTFE already causes post-monomorphization errors because some parts of CTFE can only happen after monomorphization and sometimes that can error (causing a panic during CTFE is the most obvious case).

Not sure if I would consider "you read from a static" a * new* post-monomorphization error.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 16, 2019

writing to mutable statics by first taking a reference and then writing through that reference is also only detected post monomorphization

Centril added a commit to Centril/rust that referenced this pull request Dec 24, 2019
…t, r=RalfJung

Ensure that evaluating or validating a constant never reads from a static

r? @RalfJung

as per rust-lang#66302 (comment)

This does not yet address the fact that evaluation of a constant can read from a static (under unleash-miri)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants