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

rustc: future-proof error reporting for polymorphic constants in types. #54346

Merged
merged 1 commit into from Sep 21, 2018

Conversation

eddyb
Copy link
Member

@eddyb eddyb commented Sep 19, 2018

Currently, we have 3 categories of positions where a constant can be used (const and associated const can be considered "aliases" for an expression):

  • runtime - if the function is polymorphic, we could even just warn and emit a panic
  • static - always monomorphic, so we can error at definition site
  • type-system - must enforce evaluation success where it was written

That last one is the tricky one, because we can't easily turn the presence a type with an erroring const into a runtime panic, and we'd have to do post-monomorphization errors (which we'd rather avoid).


The solution we came up with, as part of the plans for const generics, is to require successful evaluation wherever a constant shows up in a type (currently in array lengths, and values for const parameters in the future), through the WF system, which means that in certain situations (e.g. function signatures) we can assume evaluation will succeed, and require it of users (e.g. callers) instead (we've been doing this for lifetime bounds, for a long time now, and it's pretty ergonomic).

So once we do sth about #43408, this example should work, by propagating the responsability, to callers of foo::<X>, of proving std::mem::size_of::<X>() succeeds (and those callers can do the same).

pub fn foo<T>(_: [u8; std::mem::size_of::<T>()]) {}

But this one shouldn't, as there is nothing in the signature/bounds to indicate it:

pub fn bar<T>() {
    let _: [u8; std::mem::size_of::<T>()];
}

I've come across some bit of code in the compiler that ignores const-evaluation errors even when they come from a constant in a type, and I've added an ICE only when there are no other reported errors (e.g. it's fine to ignore evaluation errors if the constant doesn't even type-check).

r? @nikomatsakis cc @oli-obk @RalfJung @Centril

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 19, 2018
@eddyb
Copy link
Member Author

eddyb commented Sep 19, 2018

@bors try (for cargo check, let's see if there's anything we haven't thought of)

@bors
Copy link
Contributor

bors commented Sep 19, 2018

⌛ Trying commit 046482e with merge fcacde07b66fe513e52c41b73aef91bcc121905b...

@bors
Copy link
Contributor

bors commented Sep 19, 2018

☀️ Test successful - status-travis
State: approved= try=True

@eddyb
Copy link
Member Author

eddyb commented Sep 19, 2018

@craterbot run start=master#ff6422d7a392acfc8af28994d65af2bbaecea4f6 end=try#fcacde07b66fe513e52c41b73aef91bcc121905b mode=check-only

@craterbot
Copy link
Collaborator

👌 Experiment pr-54346 created and queued.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🚧 Experiment pr-54346 is now running on agent aws-1.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 19, 2018
@nikomatsakis
Copy link
Contributor

r=me modulo crater

@RalfJung
Copy link
Member

This PR is completely overwritten by #53821.

@RalfJung
Copy link
Member

@eddyb I think with 0a7dcf6 the other PR now subsumes this one? At least ui tests still pass.

@eddyb
Copy link
Member Author

eddyb commented Sep 19, 2018

@RalfJung Yeah, if that lands first (assuming the crater run here succeeds), I'll just close this one.

@craterbot
Copy link
Collaborator

🎉 Experiment pr-54346 is completed!
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Sep 20, 2018
@RalfJung
Copy link
Member

Both regressions say "c++: internal compiler error: Killed (program cc1plus)", looks spurious to me.

@eddyb
Copy link
Member Author

eddyb commented Sep 20, 2018

@RalfJung Yupp! We're good to go!

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Sep 20, 2018

📌 Commit 046482e has been approved by nikomatsakis

@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 Sep 20, 2018
@eddyb
Copy link
Member Author

eddyb commented Sep 20, 2018

Hang on - would it be possible to automatically search all the logs for the ICE message introduced here? (constant in type had an ignored error)
Or would it be impossible to show up as anything other than a regression? (since I used delay_span_bug so any other errors would silence it completely)

kennytm added a commit to kennytm/rust that referenced this pull request Sep 20, 2018
rustc: future-proof error reporting for polymorphic constants in types.

Currently, we have 3 categories of positions where a constant can be used (`const` and associated `const` can be considered "aliases" for an expression):
* runtime - if the function is polymorphic, we could even just warn and emit a panic
* `static` - always monomorphic, so we can error at definition site
* type-system - **must** *enforce* evaluation success where it was written

That last one is the tricky one, because we can't easily turn *the presence* a type with an erroring const into a runtime panic, and we'd have to do post-monomorphization errors (which we'd rather avoid).

<hr/>

The solution we came up with, as part of the plans for const generics, is to require successful evaluation wherever a constant shows up in a type (currently in array lengths, and values for const parameters in the future), *through* the WF system, which means that in certain situations (e.g. function signatures) we can assume evaluation *will* succeed, and require it of users (e.g. callers) instead (we've been doing this for lifetime bounds, for a long time now, and it's pretty ergonomic).

So once we do sth about rust-lang#43408, this example *should* work, by propagating the responsability, to callers of `foo::<X>`, of proving `std::mem::size_of::<X>()` succeeds (and those callers can do the same).
```rust
pub fn foo<T>(_: [u8; std::mem::size_of::<T>()]) {}
```
But this one *shouldn't*, as there is nothing in the signature/bounds to indicate it:
```rust
pub fn bar<T>() {
    let _: [u8; std::mem::size_of::<T>()];
}
```

<hr/>

I've come across some bit of code in the compiler that ignores const-evaluation errors *even when* they come from a constant in a type, and I've added an ICE *only when* there are no other reported errors (e.g. it's fine to ignore evaluation errors if the constant doesn't even type-check).

r? @nikomatsakis cc @oli-obk @RalfJung @Centril
bors added a commit that referenced this pull request Sep 20, 2018
Rollup of 15 pull requests

Successful merges:

 - #52813 (Duration div mul extras)
 - #53470 (Warn about metadata loader errors)
 - #54233 (Remove LLVM 3.9 workaround.)
 - #54257 (Switch wasm math symbols to their original names)
 - #54258 (Enable fatal warnings for the wasm32 linker)
 - #54266 (Update LLVM to fix "bool" arguments on PPC32)
 - #54290 (Switch linker for aarch64-pc-windows-msvc from LLD to MSVC)
 - #54292 (Suggest array indexing when tuple indexing on an array)
 - #54295 (A few cleanups and minor improvements to rustc/traits)
 - #54298 (miri: correctly compute expected alignment for field)
 - #54333 (Update The Book to latest)
 - #54337 (Remove unneeded clone() from tests in librustdoc)
 - #54346 (rustc: future-proof error reporting for polymorphic constants in types.)
 - #54362 (Pass --batch to gdb)
 - #54367 (Add regression test for thread local static mut borrows)
@bors bors merged commit 046482e into rust-lang:master Sep 21, 2018
@eddyb eddyb deleted the constant-horror branch September 21, 2018 06:53
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.

None yet

6 participants