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

Handle opaques in the new solver (take 2?) #111473

Merged
merged 8 commits into from
May 25, 2023
Merged

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented May 11, 2023

Implement a new strategy for handling opaques in the new solver.

First, queries now carry both their defining anchor and the opaques that were defined in the inference context at the time of canonicalization. These are both used to pre-populate the inference context used by the canonical query.

Second, use the normalizes-to goal to handle opaque types in the new solver. This means that opaques are handled like projection aliases, but with their own rules:

  • Can only define opaques if they're "defining uses" (i.e. have unique params in all their substs).
  • Can only define opaques that are from the anchor.
  • Opaque type definitions are modulo regions. So that means Opaque<'?0r> = HiddenTy1 and Opaque<?'1r> = HiddenTy2 equate HiddenTy1 and HiddenTy2 instead of defining them as different opaque type keys.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels May 11, 2023
@rust-log-analyzer

This comment has been minimized.

@aliemjay
Copy link
Member

It's not clear to me what is wrong with the current approach of having the opaque type relations in the external constraints. Do we have a write-up of this issue somewhere?

@bors
Copy link
Contributor

bors commented May 15, 2023

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

@rust-log-analyzer

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented May 22, 2023

r? @lcnr

if result.has_only_region_constraints() {
return Ok(result);
}
//if result.has_only_region_constraints() {
Copy link
Member Author

Choose a reason for hiding this comment

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

Leave a comment why

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.

very nice, "a few nits", then r=me

compiler/rustc_borrowck/src/type_check/mod.rs Outdated Show resolved Hide resolved
Comment on lines 200 to 204
ocx.infcx.tcx.fold_regions((*opaque_ty, *hidden_ty), |_, _| {
ocx.infcx.next_nll_region_var(
NllRegionVariableOrigin::Existential { from_forall: false },
)
});
Copy link
Contributor

Choose a reason for hiding this comment

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

can you use renumber_regions 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.

Region renumberer is private to rustc_borrowck::renumber :^) I don't think it offers much benefit over just folding here.

compiler/rustc_borrowck/src/type_check/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/writeback.rs Outdated Show resolved Hide resolved
compiler/rustc_infer/src/infer/nll_relate/mod.rs Outdated Show resolved Hide resolved
}
},
Reveal::All => {
let actual = tcx.type_of(opaque_ty.def_id).subst(tcx, opaque_ty.substs);
Copy link
Contributor

Choose a reason for hiding this comment

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

try add a debug assertion that predefined opaques are empty

Copy link
Member Author

Choose a reason for hiding this comment

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

:s

I would need to make the inner infcx reachable from this module, or at least expose a new EvalCtxt helper here. I don't think it's worth just yet, so I'll leave a fixme.

@@ -0,0 +1,15 @@
#![feature(type_alias_impl_trait)]
Copy link
Contributor

Choose a reason for hiding this comment

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

also add test with type params for #110601 and close that issue, i think this should fix 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.

We actually don't need to include this commit anymore. It's actually fine that our HIR typeck results have a Map<DefId, HiddenTy>, since we do the mapping from hidden ty to definition params during writeback:

let hidden_type = hidden_type.remap_generic_params_to_declaration_params(
opaque_type_key,
self.fcx.infcx.tcx,
true,
);
self.typeck_results.concrete_opaque_types.insert(opaque_type_key.def_id, hidden_type);

So I'll just land #111853 independently of this PR.

@rust-cloud-vms rust-cloud-vms bot force-pushed the opaques branch 2 times, most recently from 7b9f1ee to d576970 Compare May 23, 2023 01:06
@compiler-errors compiler-errors marked this pull request as ready for review May 23, 2023 01:08
@rustbot
Copy link
Collaborator

rustbot commented May 23, 2023

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@compiler-errors
Copy link
Member Author

Alright this is ready for review once again, @lcnr. Left a bunch of comments and a few more modifications. Should be very easy to review now.

@rust-log-analyzer

This comment has been minimized.

@@ -1041,6 +1048,57 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
checker
}

pub(super) fn register_predefined_opaques_in_new_solver(&mut self) {
Copy link
Contributor

@lcnr lcnr May 24, 2023

Choose a reason for hiding this comment

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

how is that fine (for TAIT)? 🤔 aren't the preregistered opaque types in the wrong environment now, they have the generic params of the opaque while in the context of the defining scope?

Copy link
Contributor

Choose a reason for hiding this comment

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

as a thought which also deals with rust-lang/trait-system-refactor-initiative#17

long term we could maybe end up with the following to replace DefiningAnchor:

enum DefineOpaques {
    // the current `DefiningAnchor::Bind`
    Yes(&'tcx List<DefId>),
    // We do not allow any new definitions for our opaques,
    // but when using an opaque with `DefId`, we instantiate the
    // inferred hidden type with these substs.
    //
    // This doesn't quite work because of lifetimes, but we can
    // figure something out there :grin:
    No(SomeMapCollection<DefId, EarlyBinder<Ty<'tcx>>),
}

anyways, your PR works for RPIT so I wouldn't mind us landing this as long as we put a fixme 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.

You're absolutely right lol. We shouldn't map RPITITs back to their declaration params during writeback if we're in the new trait solver. 🤦

I'll add a fixme anyways.

@@ -356,8 +354,11 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
}),
);
ecx.add_goal(normalizes_to_goal);
let _ = ecx.try_evaluate_added_goals()?;
let _ = ecx.try_evaluate_added_goals().inspect_err(|_| {
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
let _ = ecx.try_evaluate_added_goals().inspect_err(|_| {
let _certainty = ecx.try_evaluate_added_goals().inspect_err(|_| {

@lcnr
Copy link
Contributor

lcnr commented May 24, 2023

I guess r=me with FIXME for #111473 (comment)

@bors
Copy link
Contributor

bors commented May 25, 2023

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

@compiler-errors
Copy link
Member Author

compiler-errors commented May 25, 2023

@bors r=lcnr

Follow-ups:

@bors
Copy link
Contributor

bors commented May 25, 2023

📌 Commit dd98198 has been approved by lcnr

It is now in the queue for this repository.

@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 May 25, 2023
@bors
Copy link
Contributor

bors commented May 25, 2023

⌛ Testing commit dd98198 with merge eb9da7b...

@bors
Copy link
Contributor

bors commented May 25, 2023

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing eb9da7b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 25, 2023
@bors bors merged commit eb9da7b into rust-lang:master May 25, 2023
@rustbot rustbot added this to the 1.71.0 milestone May 25, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (eb9da7b): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.6%, 0.6%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.1% [3.1%, 3.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 646.729s -> 646.591s (-0.02%)

@lcnr lcnr mentioned this pull request May 31, 2023
11 tasks
@compiler-errors compiler-errors deleted the opaques branch August 11, 2023 20:12
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants