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

generic_const_exprs: use thir for abstract consts instead of mir #88709

Merged
merged 18 commits into from
Sep 12, 2021

Conversation

BoxyUwU
Copy link
Member

@BoxyUwU BoxyUwU commented Sep 7, 2021

Changes AbstractConst building to use thir instead of mir so that there's less chance of consts unifying when they shouldn't because lowering to mir dropped information (see abstract-consts-as-cast-5.rs test)

r? @lcnr

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 7, 2021
@BoxyUwU BoxyUwU force-pushed the thir-abstract-const branch from f0064e4 to 578387d Compare September 7, 2021 02:13
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

💖 💖 💖

a bunch of small nits which might even be slightly too pedantic.

The error messages for overly complex constants are a bit worse now, but improving them can be done in followup PRs.

compiler/rustc_middle/src/mir/query.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/thir/visit.rs Outdated Show resolved Hide resolved
self.nodes.push(WorkNode { node, span, used: false })
}
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
self.is_poly |= expr.ty.definitely_has_param_types_or_consts(self.tcx);
Copy link
Contributor

Choose a reason for hiding this comment

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

ExprKind::Cast also contains a Ty and afaict you don't look at thir::Pat at all 🤔

maybe it makes sense to add a fn visit_ty to the Visitor trait and only manually implement visit_ty and visit_const here.

Copy link
Member Author

Choose a reason for hiding this comment

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

ExprKind::Cast just contains an ExprId the type of the cast is from expr.ty so that's correct.

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 did forget about thir::Pat i think I thought it was unnecessary as i dont think you can currently use a pattern in a generic constant but that doesn't matter for checking this

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'm not sure that visiting patterns is actually important, I cant think of anything in patterns that could be generic that wouldnt be handled by either visit_const from when a pattern is a constant or visit_expr for what's being matched on/let = on

Copy link
Contributor

Choose a reason for hiding this comment

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

woups, meant ExprKind::Call 😅

I personally don't think thir::Pat is too important either, or well, only for is_polymorphic (which we should be able to remove from mir::Body now). But the current impls seems fragile if we ever add a Ty to some other ExprKind or something, so i would like a safer approach here

Copy link
Member Author

@BoxyUwU BoxyUwU Sep 7, 2021

Choose a reason for hiding this comment

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

oh right, I assumed that the ty in ExprKind::Call was the same as the type of the fun expr which is why I didnt visit that

edit: looking at code it does infact seem so, it's fine to ignore the ty in ExprKind::Call when checking if the thir is polymorphic as we'll visit that ty when we visit the fun expr

Copy link
Member Author

@BoxyUwU BoxyUwU Sep 8, 2021

Choose a reason for hiding this comment

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

I changed how we check if thir is polymorphic to explicitly match on ExprKind and PatKind some of the arms are doing definitely_has_param_types_or_consts but im not really sure it actually makes any difference to be doing so

edit: yeah as far as I can tell these match arms arent doing anything so I've removed them which leaves us with two match statements that exhaustively match on ExprKind and PatKind without actually doing anything which seems incredibly silly but does solve the problem of "what happens if someone adds a Ty field to an ExprKind variant"

think it might just be smarter to add tests that we give an "unsupported operation in generic constant" error for each of these variants

Copy link
Contributor

Choose a reason for hiding this comment

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

two match statements that exhaustively match on ExprKind and PatKind without actually doing anything which seems incredibly silly

🤔 that doesn't make me too happy. not sure how easy and useful it is to add tests for this, but I think that having an exhaustive match here is more annoying than helpful '^^

imo its fine to just remove the match, we have enough time on nightly to see whether this is a problem

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the thir-abstract-const branch 2 times, most recently from d08155a to afa9b39 Compare September 8, 2021 19:01
@bors
Copy link
Contributor

bors commented Sep 9, 2021

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

@BoxyUwU BoxyUwU force-pushed the thir-abstract-const branch from afa9b39 to 25c5033 Compare September 9, 2021 00:44
compiler/rustc_metadata/src/rmeta/encoder.rs Outdated Show resolved Hide resolved
Comment on lines +18 to +22
fn foo<const N: Foo>(a: Evaluatable<{ N + N }>) {
bar::<{ std::ops::Add::add(N, N) }>();
}

fn bar<const N: Foo>() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn foo<const N: Foo>(a: Evaluatable<{ N + N }>) {
bar::<{ std::ops::Add::add(N, N) }>();
}
fn bar<const N: Foo>() {}
fn foo<const N: Foo>(a: Evaluatable<{ N + N }>) {
bar::<{ std::ops::Add::add(N, N) }>();
}
fn bar<const N: Foo>() {}
fn baz<const N: usize>(a: [u8; N + N]) {
biz::<{ std::ops::Add::add(N, N) }>();
}
fn biz<const N: usize>() {}

interested in seeing this tested for builtin types

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm yeah Add::add(N, N) doesnt unify with N + N for builtin types

Copy link
Member Author

@BoxyUwU BoxyUwU Sep 9, 2021

Choose a reason for hiding this comment

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

I think we could totally support this by just making Node::BinOp(..) for operator functions but probably ought to do that in a separate PR rather than alongside this mir -> thir refactor (or alternatively get rid of Node::BinOp and just lower to Node::Function(..) but either way there is probably solutions)

@lcnr
Copy link
Contributor

lcnr commented Sep 9, 2021

some more nits and the match in IsThirPolymorphic, after that this seems ready for merge to me

@BoxyUwU BoxyUwU force-pushed the thir-abstract-const branch from 25c5033 to ee48c18 Compare September 9, 2021 14:42
@BoxyUwU BoxyUwU force-pushed the thir-abstract-const branch from ee48c18 to 8295e4a Compare September 9, 2021 14:44
@BoxyUwU BoxyUwU marked this pull request as ready for review September 9, 2021 17:54
@lcnr
Copy link
Contributor

lcnr commented Sep 10, 2021

@rust-lang/project-const-generics we're now using thir instead of mir for the unification of generic constants

@bors r+

@bors
Copy link
Contributor

bors commented Sep 10, 2021

📌 Commit 8295e4a has been approved by lcnr

@bors
Copy link
Contributor

bors commented Sep 10, 2021

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@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 10, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Sep 11, 2021
generic_const_exprs: use thir for abstract consts instead of mir

not sure if this is handling some of the weirder `thir::ExprKind` correctly in `recurse_build` (it probably isnt)

r? `@lcnr`
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Sep 11, 2021
generic_const_exprs: use thir for abstract consts instead of mir

not sure if this is handling some of the weirder `thir::ExprKind` correctly in `recurse_build` (it probably isnt)

r? ``@lcnr``
Manishearth added a commit to Manishearth/rust that referenced this pull request Sep 12, 2021
generic_const_exprs: use thir for abstract consts instead of mir

Changes `AbstractConst` building to use `thir` instead of `mir` so that there's less chance of consts unifying when they shouldn't because lowering to mir dropped information (see `abstract-consts-as-cast-5.rs` test)

r? `@lcnr`
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 12, 2021
…arth

Rollup of 7 pull requests

Successful merges:

 - rust-lang#88336 ( Detect stricter constraints on gats where clauses in impls vs trait)
 - rust-lang#88677 (rustc: Remove local variable IDs from `Export`s)
 - rust-lang#88699 (Remove extra unshallow from cherry-pick checker)
 - rust-lang#88709 (generic_const_exprs: use thir for abstract consts instead of mir)
 - rust-lang#88711 (Rework DepthFirstSearch API)
 - rust-lang#88810 (rustdoc: Cleanup `clean` part 1)
 - rust-lang#88813 (explicitly link to external `ena` docs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f5ac5ca into rust-lang:master Sep 12, 2021
@rustbot rustbot added this to the 1.57.0 milestone Sep 12, 2021
@Manishearth
Copy link
Member

The rollup caused an instruction count increase on perf, do you think that's due to this PR?

#88881

https://perf.rust-lang.org/compare.html?start=9ef27bf7dc50a8b51435579b4f2e86f7ee3f7a94&end=c7dbe7a830100c70d59994fd940bf75bb6e39b39

@BoxyUwU
Copy link
Member Author

BoxyUwU commented Sep 12, 2021

Im not sure how this would cause a perf regression given all the changed code is gated behind feature(generic_const_exprs)

@lcnr lcnr added A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Dec 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` 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.

8 participants