Skip to content

Postpone the evaluation of constant expressions that depend on inference variables - #90023

Merged
bors merged 4 commits into
rust-lang:masterfrom
b-naber:postpone_const_eval_infer_vars
Dec 5, 2021
Merged

Postpone the evaluation of constant expressions that depend on inference variables#90023
bors merged 4 commits into
rust-lang:masterfrom
b-naber:postpone_const_eval_infer_vars

Conversation

@b-naber

@b-naberb-naber commented Oct 18, 2021

Copy link
Copy Markdown
Contributor

Previously delay_span_bug calls were triggered once an inference variable was included in the substs of a constant that was to be evaluated. Some of these would merely have resulted in trait candidates being rejected, hence no real error was ever encountered, but the triggering of the delay_span_bug then caused an ICE in later stages of the compiler due to no error ever occurring.
We now postpone the evaluation of these constants, so any trait obligation fulfillment will simply stall on this constant and the existing type inference machinery of the compiler handles any type errors if present.

Fixes#89320
Fixes#89146
Fixes#87964
Fixes#87470
Fixes#83288
Fixes#83249
Fixes#90654

I want to thank @BoxyUwU for cooperating on this and for providing some help.

r? @lcnr maybe?

@rust-highfive

Copy link
Copy Markdown
Contributor

Some changes occured to the CTFE / Miri engine

cc @rust-lang/miri

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 18, 2021
@rust-log-analyzer

This comment has been minimized.

Comment threadcompiler/rustc_infer/src/infer/mod.rs Outdated
@BoxyUwUBoxyUwU self-assigned this Oct 18, 2021
@b-naber
b-naberforce-pushed the postpone_const_eval_infer_vars branch 2 times, most recently from effd201 to afa34ceCompareOctober 18, 2021 17:57

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should be erasing lifetimes in param_env and substs at the start of this fn

@lcnrlcnrOct 18, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

y, with this PR we can erase lifetimes and don't have to canonicalize anymore

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ok, I'll change that. Can you explain why we should do this? Is it just that we don't need any lifetime information anymore during codegen (which the evaluation of constants loosely speaking is)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it just that we don't need any lifetime information anymore during codegen

yes, const eval and codegen (which should pretty much behave the same) do not depend on lifetime information so it is fine to erase them. That also improves cashing as all lifetime params are always 'erased.

Comment threadsrc/test/ui/const-generics/issues/issue-89320.rs Outdated
@b-naber
b-naberforce-pushed the postpone_const_eval_infer_vars branch from afa34ce to 42695c2CompareOctober 18, 2021 20:58
@lcnr

lcnr commented Oct 28, 2021

Copy link
Copy Markdown
Contributor

forgot to write this when I first reviewed this:

the change looks good to me but I think we should not land this if we ever intend to revert this PR, as in that case it simply masks bugs we will have to fix then. My only concern here is that anonymous constants can refer to unused generic arguments, so by not trying to evaluate them we will probably prevent some really rare cases from compiling. Need to take some time to figure out the greater picture here, so it might take a bit until this PR gets merged.

@apirainoapiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 28, 2021

@nikomatsakisnikomatsakis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me with the change below

Comment on lines 1591 to 1604

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suggest we re-order this as follows:

Suggested change
let param_env = self.tcx.erase_regions(param_env);
letmut substs = unevaluated.substs(self.tcx);
substs = self.tcx.erase_regions(substs);
substs = self.resolve_vars_if_possible(substs);
// Postpone the evaluation of constants whose substs depend on inference
// variables
if substs.has_infer_types_or_consts(){
returnErr(ErrorHandled::TooGeneric);
}
let unevaluated = ty::Unevaluated{
def: unevaluated.def,
substs_:Some(substs),
promoted: unevaluated.promoted,
};
letmut substs = unevaluated.substs(self.tcx);
substs = self.resolve_vars_if_possible(substs);
// Postpone the evaluation of constants whose substs depend on inference
// variables
if substs.has_infer_types_or_consts(){
returnErr(ErrorHandled::TooGeneric);
}
let param_env_erased = self.tcx.erase_regions(param_env);
let substs_erased = self.tcx.erase_regions(substs);
let unevaluated = ty::Unevaluated{
def: unevaluated.def,
substs_:Some(substs_erased),
promoted: unevaluated.promoted,
};
// The return value is the evaluated value which doesn't contain any reference to inference
// variables, thus we don't need to substitute back the original values.
self.tcx.const_eval_resolve(param_env, unevaluated, span)

It is necessary to resolve variables before erasing because an inference variable may resolve to an &T of some kind.

@rust-log-analyzer

This comment has been minimized.

@b-naber
b-naberforce-pushed the postpone_const_eval_infer_vars branch from effbfa8 to 3a7af1bCompareNovember 7, 2021 21:40
@rust-log-analyzer

This comment has been minimized.

@jackh726

Copy link
Copy Markdown
Member

@bors r=nikomatsakis

@bors

bors commented Dec 4, 2021

Copy link
Copy Markdown
Collaborator

📌 Commit 37ed2db has been approved by nikomatsakis

@borsbors 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 Dec 4, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 5, 2021
…askrgr
Rollup of 6 pull requests
Successful merges:
- rust-lang#89642 (environ on macos uses directly libc which has the correct signature.)
- rust-lang#90022 (Explain why `Self` is invalid in generic parameters)
- rust-lang#90023 (Postpone the evaluation of constant expressions that depend on inference variables)
- rust-lang#91215 (Implement VecDeque::retain_mut)
- rust-lang#91355 (std: Stabilize the `thread_local_const_init` feature)
- rust-lang#91528 (LLVM support .insn directive)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 1f2a26e into rust-lang:masterDec 5, 2021
@rustbotrustbot added this to the 1.59.0 milestone Dec 5, 2021
@b-naber
b-naber deleted the postpone_const_eval_infer_vars branch December 5, 2021 22:49
@jackh726

Copy link
Copy Markdown
Member

@b-naber any chance I can enlist you to go through the above issues and decide which are "duplicates" and which need a test?

@b-naber

Copy link
Copy Markdown
ContributorAuthor

@b-naber any chance I can enlist you to go through the above issues and decide which are "duplicates" and which need a test?

Of course, I'll do that tomorrow.

@b-naberb-naber mentioned this pull request Dec 8, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 9, 2021
…al, r=jackh726
Add tests fixed by rust-lang#90023
The following issues were fixed by rust-lang#90023Fixesrust-lang#79674Fixesrust-lang#83765Fixesrust-lang#86033Fixesrust-lang#90318Fixesrust-lang#88468
The following issues were duplicates of rust-lang#90654Fixesrust-lang#86850Fixesrust-lang#89022
r? `@jackh726`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 10, 2021
…al, r=jackh726
Add tests fixed by rust-lang#90023
The following issues were fixed by rust-lang#90023Fixesrust-lang#79674Fixesrust-lang#83765Fixesrust-lang#86033Fixesrust-lang#90318Fixesrust-lang#88468
The following issues were duplicates of rust-lang#90654Fixesrust-lang#86850Fixesrust-lang#89022
r? ``@jackh726``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 10, 2021
…al, r=jackh726
Add tests fixed by rust-lang#90023
The following issues were fixed by rust-lang#90023Fixesrust-lang#79674Fixesrust-lang#83765Fixesrust-lang#86033Fixesrust-lang#90318Fixesrust-lang#88468
The following issues were duplicates of rust-lang#90654Fixesrust-lang#86850Fixesrust-lang#89022
r? ```@jackh726```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 10, 2021
…al, r=jackh726
Add tests fixed by rust-lang#90023
The following issues were fixed by rust-lang#90023Fixesrust-lang#79674Fixesrust-lang#83765Fixesrust-lang#86033Fixesrust-lang#90318Fixesrust-lang#88468
The following issues were duplicates of rust-lang#90654Fixesrust-lang#86850Fixesrust-lang#89022
r? ````@jackh726````
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 11, 2021
…askrgr
Rollup of 11 pull requests
Successful merges:
- rust-lang#91668 (Remove the match on `ErrorKind::Other`)
- rust-lang#91678 (Add tests fixed by rust-lang#90023)
- rust-lang#91679 (Move core/stream/stream/mod.rs to core/stream/stream.rs)
- rust-lang#91681 (fix typo in `intrinsics::raw_eq` docs)
- rust-lang#91686 (Fix `Vec::reserve_exact` documentation)
- rust-lang#91697 (Delete Utf8Lossy::from_str)
- rust-lang#91706 (Add unstable book entries for parts of asm that are not being stabilized)
- rust-lang#91709 (Replace iterator-based set construction by *Set::From<[T; N]>)
- rust-lang#91716 (Improve x.py logging and defaults a bit more)
- rust-lang#91747 (Add pierwill to .mailmap)
- rust-lang#91755 (Fix since attribute for const_linked_list_new feature)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@lcnrlcnr added the A-const-generics Area: const generics (parameters and arguments) label Dec 11, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 18, 2022
…ce, r=lcnr
Directly use ConstValue for single literals in blocks
Addresses the minimal repro in rust-lang#92186, but doesn't fix the underlying problem (which would be solved by solving the anon subst problem afaict).
I do, however, think that it makes sense in general to treat single literals in anon blocks as const values directly, especially in light of the problem that the issue refers to (anon const evaluation being postponed until infer variables in substs can be resolved, which was introduced by rust-lang#90023), i.e. while we do get warnings for those unnecessary braces, we should try to avoid errors caused by those braces if possible.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-const-genericsArea: const generics (parameters and arguments)S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

13 participants

@b-naber@rust-highfive@rust-log-analyzer@lcnr@jackh726@bors@Alexendoo@nikomatsakis@RalfJung@oli-obk@BoxyUwU@apiraino@rustbot