Skip to content

Support non-defining uses in HIR typeck #145711

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Aug 21, 2025

builds on #145706

This changes they impl of NormalizesTo for opaque types to be structural during HIR typeck. It currently equates region variables of the opaque type key with existing entries which can result in spurious leak check errors and also results in mismatches with MIR borrowck, theoretically causing ICE.

TODO

r? @BoxyUwU

@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 Aug 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 21, 2025

Some changes occurred to the core trait solver

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

@@ -832,12 +832,12 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
//
// We erase regions when doing this during HIR typeck.
let this = match defining_scope_kind {
DefiningScopeKind::HirTypeck => tcx.erase_regions(self),
DefiningScopeKind::HirTypeck => fold_regions(tcx, self, |_, _| tcx.lifetimes.re_erased),
Copy link
Member

Choose a reason for hiding this comment

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

comment about why we don't want to anonymize bound vars

if opaque_type_key.def_id != def_id {
continue;
}
if let Err(err) = check_opaque_type_parameter_valid(
Copy link
Member

Choose a reason for hiding this comment

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

I think this framing of "check X" with it returning an Err(err) is really confusing now that we have non defining uses. It's not an error to have a non defining use and its also not "invalid" to have weird arguments to opaques, its just a decision about whether its a defining use or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rename to opaque_type_has_defining_use_args, NonDefiningUseReason

///
/// A use during HIR typeck is defining if all non-lifetime arguments are
/// unique generic parameters and the hidden type does not reference any
/// unconstrained inference variables.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// unconstrained inference variables.
/// inference variables.

Clarifying unconstrained is just confusing to me. imo in general in the compiler the default assumption is that we are talking about the resolved forms of types.

Comment on lines +60 to +63
let mut non_defining_use = None;
let mut unconstrained_hidden_type = None;
let mut found_defining_use = false;
for &(opaque_type_key, hidden_type) in opaque_types {
Copy link
Member

Choose a reason for hiding this comment

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

i find the structure of this loop + the variables declared ahead of time to be quite confusing but I'm not sure I can articulate what my problem with it is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

enum UsageKind {
    None,
    NonDefiningUse(...),
    UnconstrainedHiddenType(..),
    HasDefiningUse,
}

with usage_kind.merge(consider_opaque_type_use(...)): in the loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add comment why no short circuit, it's only diagnostics :3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead, actually short circuit and add new cause kind to the relate in apply_uses.

)
.emit()
} else if let Some((_, hidden_type)) = non_defining_use {
tcx.dcx().span_err(hidden_type.span, "non-defining use in the defining scope")
Copy link
Member

Choose a reason for hiding this comment

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

non defining uses are not errors. this should be "non defining use without a defining use"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just emit "no defining use" + "note, does nothave unique universal generic arguments" or sth

Comment on lines +537 to +542
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
for (&def_id, &hidden_type) in &fcx_typeck_results.concrete_opaque_types {
assert!(!hidden_type.has_infer());
self.typeck_results.concrete_opaque_types.insert(def_id, hidden_type);
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
for (&def_id, &hidden_type) in &fcx_typeck_results.concrete_opaque_types {
assert!(!hidden_type.has_infer());
self.typeck_results.concrete_opaque_types.insert(def_id, hidden_type);
}
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
assert!(fcx_typeck_results.concrete_opaque_types.values().all(|hidden_ty| !hidden_ty.has_infer()))
assert_eq!(self.typeck_results.concrete_opaque_types.len(), 0)
self.typeck_results.concrete_opaque_types = fcx_typeck_results.concrete_opaque_types.clone();

does your code differ in some way from just cloning concrete_opaque_types? more performant because you can do the assertion while "cloning" it? we expect for other parts of write back to insert stuff into opaque type storage for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ye, mem::take

Comment on lines 50 to 51
self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
Copy link
Member

Choose a reason for hiding this comment

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

can you add a comment saying "if we're not in the defining scope treat the alias as rigid" or something, I got tripped up by the let else as usual

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ye sure

Comment on lines +54 to +56
// We structurally normalize the args so that we're able to detect defining uses
// later on. It also reduces the amount of duplicate definitions in the
// `opaque_type_storage`.
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me that "reduces the amount of duplicate definitions in the opaque type storage" is actually a good thing.

This would improve inference as a usage of Opaque<T> and Opaque<<T as Trait>::Self> now share opaque type storage entries. This then means that the exact behaviour of structurally_normalize_ty can affect whether normalization of an opaque results in an infer var or some partially-inferred hidden type from a previous usage.

Like the fact that structurally normalize ty is actually "deeply normalize except for aliases with bound vars" is now something which affects the results of opaque type normalization.

This makes me feel stronger that we should have normalization of opaques to look up all the existing usages and infer a hidden type based off that as it would mitigate this. Having opaque type keys differ in normalization doesn't affect inference if we allow usages to affect eachother's hidden types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add comment

@BoxyUwU BoxyUwU 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 Aug 21, 2025
@bors
Copy link
Collaborator

bors commented Aug 23, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

4 participants