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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ fn apply_computed_concrete_opaque_types<'tcx>(
let locations = Locations::All(hidden_type.span);
if let Err(guar) = fully_perform_op_raw(
infcx,
root_cx.root_def_id(),
body,
universal_regions,
region_bound_pairs,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_borrowck/src/root_cx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
}
}

pub(super) fn root_def_id(&self) -> LocalDefId {
self.root_def_id
}

/// Collect all defining uses of opaque types inside of this typeck root. This
/// expects the hidden type to be mapped to the definition parameters of the opaque
/// and errors if we end up with distinct hidden types.
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use rustc_errors::ErrorGuaranteed;
use rustc_hir::def_id::LocalDefId;
use rustc_infer::infer::canonical::Canonical;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_middle::bug;
Expand All @@ -23,6 +24,7 @@ use crate::universal_regions::UniversalRegions;
#[instrument(skip(infcx, constraints, op), level = "trace")]
pub(crate) fn fully_perform_op_raw<'tcx, R: fmt::Debug, Op>(
infcx: &BorrowckInferCtxt<'tcx>,
root_def_id: LocalDefId,
body: &Body<'tcx>,
universal_regions: &UniversalRegions<'tcx>,
region_bound_pairs: &RegionBoundPairs<'tcx>,
Expand All @@ -39,7 +41,7 @@ where
let old_universe = infcx.universe();

let TypeOpOutput { output, constraints: query_constraints, error_info } =
op.fully_perform(infcx, locations.span(body))?;
op.fully_perform(infcx, root_def_id, locations.span(body))?;
if cfg!(debug_assertions) {
let data = infcx.take_and_reset_region_constraints();
if !data.is_empty() {
Expand All @@ -52,6 +54,7 @@ where
if let Some(data) = query_constraints {
constraint_conversion::ConstraintConversion::new(
infcx,
root_def_id,
universal_regions,
region_bound_pairs,
infcx.param_env,
Expand Down Expand Up @@ -103,6 +106,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
{
fully_perform_op_raw(
self.infcx,
self.root_cx.root_def_id(),
self.body,
self.universal_regions,
self.region_bound_pairs,
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_borrowck/src/type_check/constraint_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{ClosureOutlivesSubject, ClosureRegionRequirements, ConstraintCategor

pub(crate) struct ConstraintConversion<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
root_def_id: LocalDefId,
universal_regions: &'a UniversalRegions<'tcx>,
/// Each RBP `GK: 'a` is assumed to be true. These encode
/// relationships like `T: 'a` that are added via implicit bounds
Expand All @@ -46,6 +47,7 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
pub(crate) fn new(
infcx: &'a InferCtxt<'tcx>,
root_def_id: LocalDefId,
universal_regions: &'a UniversalRegions<'tcx>,
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
param_env: ty::ParamEnv<'tcx>,
Expand All @@ -57,6 +59,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
) -> Self {
Self {
infcx,
root_def_id,
universal_regions,
region_bound_pairs,
param_env,
Expand Down Expand Up @@ -286,8 +289,11 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
ConstraintCategory<'tcx>,
)>,
) -> Ty<'tcx> {
match self.param_env.and(DeeplyNormalize { value: ty }).fully_perform(self.infcx, self.span)
{
match self.param_env.and(DeeplyNormalize { value: ty }).fully_perform(
self.infcx,
self.root_def_id,
self.span,
) {
Ok(TypeOpOutput { output: ty, constraints, .. }) => {
// FIXME(higher_ranked_auto): What should we do with the assumptions here?
if let Some(QueryRegionConstraints { outlives, assumptions: _ }) = constraints {
Expand Down
23 changes: 15 additions & 8 deletions compiler/rustc_borrowck/src/type_check/free_region_relations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_infer::infer::region_constraints::GenericKind;
Expand Down Expand Up @@ -48,12 +49,14 @@ pub(crate) struct CreateResult<'tcx> {

pub(crate) fn create<'tcx>(
infcx: &InferCtxt<'tcx>,
root_def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
universal_regions: UniversalRegions<'tcx>,
constraints: &mut MirTypeckRegionConstraints<'tcx>,
) -> CreateResult<'tcx> {
UniversalRegionRelationsBuilder {
infcx,
root_def_id,
param_env,
constraints,
universal_regions,
Expand Down Expand Up @@ -178,6 +181,7 @@ impl UniversalRegionRelations<'_> {

struct UniversalRegionRelationsBuilder<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
root_def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
universal_regions: UniversalRegions<'tcx>,
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
Expand Down Expand Up @@ -263,7 +267,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
let TypeOpOutput { output: norm_ty, constraints: constraints_normalize, .. } =
param_env
.and(DeeplyNormalize { value: ty })
.fully_perform(self.infcx, span)
.fully_perform(self.infcx, self.root_def_id, span)
.unwrap_or_else(|guar| TypeOpOutput {
output: Ty::new_error(self.infcx.tcx, guar),
constraints: None,
Expand Down Expand Up @@ -298,8 +302,9 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
// Add implied bounds from impl header.
if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst) {
for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(defining_ty_def_id)) {
let result: Result<_, ErrorGuaranteed> =
param_env.and(DeeplyNormalize { value: ty }).fully_perform(self.infcx, span);
let result: Result<_, ErrorGuaranteed> = param_env
.and(DeeplyNormalize { value: ty })
.fully_perform(self.infcx, self.root_def_id, span);
let Ok(TypeOpOutput { output: norm_ty, constraints: c, .. }) = result else {
continue;
};
Expand All @@ -316,6 +321,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
for c in constraints {
constraint_conversion::ConstraintConversion::new(
self.infcx,
self.root_def_id,
&self.universal_regions,
&self.region_bound_pairs,
param_env,
Expand Down Expand Up @@ -353,10 +359,11 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
output: normalized_outlives,
constraints: constraints_normalize,
error_info: _,
}) = self
.param_env
.and(DeeplyNormalize { value: outlives })
.fully_perform(self.infcx, span)
}) = self.param_env.and(DeeplyNormalize { value: outlives }).fully_perform(
self.infcx,
self.root_def_id,
span,
)
else {
self.infcx.dcx().delayed_bug(format!("could not normalize {outlives:?}"));
return;
Expand All @@ -383,7 +390,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
let TypeOpOutput { output: bounds, constraints, .. } = self
.param_env
.and(type_op::ImpliedOutlivesBounds { ty })
.fully_perform(self.infcx, span)
.fully_perform(self.infcx, self.root_def_id, span)
.map_err(|_: ErrorGuaranteed| debug!("failed to compute implied bounds {:?}", ty))
.ok()?;
debug!(?bounds, ?constraints);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ impl<'tcx> LivenessContext<'_, '_, 'tcx> {

let op = typeck.infcx.param_env.and(DropckOutlives { dropped_ty });

match op.fully_perform(typeck.infcx, DUMMY_SP) {
match op.fully_perform(typeck.infcx, typeck.root_cx.root_def_id(), DUMMY_SP) {
Ok(TypeOpOutput { output, constraints, .. }) => {
DropData { dropck_result: output, region_constraint_data: constraints }
}
Expand Down
27 changes: 16 additions & 11 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ pub(crate) fn type_check<'tcx>(
region_bound_pairs,
normalized_inputs_and_output,
known_type_outlives_obligations,
} = free_region_relations::create(infcx, infcx.param_env, universal_regions, &mut constraints);
} = free_region_relations::create(
infcx,
root_cx.root_def_id(),
infcx.param_env,
universal_regions,
&mut constraints,
);

let pre_obligations = infcx.take_registered_region_obligations();
assert!(
Expand Down Expand Up @@ -406,6 +412,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

constraint_conversion::ConstraintConversion::new(
self.infcx,
self.root_cx.root_def_id(),
self.universal_regions,
self.region_bound_pairs,
self.infcx.param_env,
Expand Down Expand Up @@ -2458,9 +2465,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
args: GenericArgsRef<'tcx>,
locations: Locations,
) -> ty::InstantiatedPredicates<'tcx> {
let root_def_id = self.root_cx.root_def_id();
if let Some(closure_requirements) = &self.root_cx.closure_requirements(def_id) {
constraint_conversion::ConstraintConversion::new(
self.infcx,
root_def_id,
self.universal_regions,
self.region_bound_pairs,
self.infcx.param_env,
Expand All @@ -2473,9 +2482,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
.apply_closure_requirements(closure_requirements, def_id, args);
}

// Now equate closure args to regions inherited from `typeck_root_def_id`. Fixes #98589.
let typeck_root_def_id = tcx.typeck_root_def_id(self.body.source.def_id());
let typeck_root_args = ty::GenericArgs::identity_for_item(tcx, typeck_root_def_id);
// Now equate closure args to regions inherited from `root_def_id`. Fixes #98589.
let typeck_root_args = ty::GenericArgs::identity_for_item(tcx, root_def_id);

let parent_args = match tcx.def_kind(def_id) {
// We don't want to dispatch on 3 different kind of closures here, so take
Expand Down Expand Up @@ -2550,17 +2558,14 @@ impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
fn fully_perform(
mut self,
infcx: &InferCtxt<'tcx>,
root_def_id: LocalDefId,
span: Span,
) -> Result<TypeOpOutput<'tcx, Self>, ErrorGuaranteed> {
let (mut output, region_constraints) = scrape_region_constraints(
infcx,
|ocx| {
let (mut output, region_constraints) =
scrape_region_constraints(infcx, root_def_id, "InstantiateOpaqueType", span, |ocx| {
ocx.register_obligations(self.obligations.clone());
Ok(())
},
"InstantiateOpaqueType",
span,
)?;
})?;
self.region_constraints = Some(region_constraints);
output.error_info = Some(self);
Ok(output)
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,3 +2053,29 @@ pub(super) fn check_coroutine_obligations(

Ok(())
}

pub(super) fn check_potentially_region_dependent_goals<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> Result<(), ErrorGuaranteed> {
if !tcx.next_trait_solver_globally() {
return Ok(());
}
let typeck_results = tcx.typeck(def_id);
let param_env = tcx.param_env(def_id);

// We use `TypingMode::Borrowck` as we want to use the opaque types computed by HIR typeck.
let typing_mode = TypingMode::borrowck(tcx, def_id);
let infcx = tcx.infer_ctxt().ignoring_regions().build(typing_mode);
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
for (predicate, cause) in &typeck_results.potentially_region_dependent_goals {
let predicate = fold_regions(tcx, *predicate, |_, _| {
infcx.next_region_var(RegionVariableOrigin::Misc(cause.span))
});
ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate));
}

let errors = ocx.select_all_or_error();
debug!(?errors);
if errors.is_empty() { Ok(()) } else { Err(infcx.err_ctxt().report_fulfillment_errors(errors)) }
}
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub(super) fn provide(providers: &mut Providers) {
collect_return_position_impl_trait_in_trait_tys,
compare_impl_item: compare_impl_item::compare_impl_item,
check_coroutine_obligations: check::check_coroutine_obligations,
check_potentially_region_dependent_goals: check::check_potentially_region_dependent_goals,
check_type_wf: wfcheck::check_type_wf,
check_well_formed: wfcheck::check_well_formed,
..*providers
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl<'tcx> TaitConstraintLocator<'tcx> {
}

fn non_defining_use_in_defining_scope(&mut self, item_def_id: LocalDefId) {
// We make sure that all opaque types get defined while
// type checking the defining scope, so this error is unreachable
// with the new solver.
assert!(!self.tcx.next_trait_solver_globally());
let guar = self.tcx.dcx().emit_err(TaitForwardCompat2 {
span: self
.tcx
Expand Down Expand Up @@ -252,9 +256,7 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
} else if let Some(hidden_ty) = tables.concrete_opaque_types.get(&def_id) {
hidden_ty.ty
} else {
// FIXME(-Znext-solver): This should not be necessary and we should
// instead rely on inference variable fallback inside of typeck itself.

assert!(!tcx.next_trait_solver_globally());
// We failed to resolve the opaque type or it
// resolves to itself. We interpret this as the
// no values of the hidden type ever being constructed,
Expand All @@ -273,6 +275,7 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
if let Err(guar) = hir_ty.error_reported() {
Ty::new_error(tcx, guar)
} else {
assert!(!tcx.next_trait_solver_globally());
hir_ty
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub(crate) struct UnconstrainedOpaqueType {
#[derive(Diagnostic)]
#[diag(hir_analysis_tait_forward_compat2)]
#[note]
pub(crate) struct TaitForwardCompat2 {
pub struct TaitForwardCompat2 {
#[primary_span]
pub span: Span,
#[note(hir_analysis_opaque)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod coherence;
mod collect;
mod constrained_generic_params;
mod delegation;
mod errors;
pub mod errors;
pub mod hir_ty_lowering;
pub mod hir_wf_check;
mod impl_wf_check;
Expand Down
24 changes: 8 additions & 16 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use rustc_hir_analysis::check::{check_abi, check_custom_abi};
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
use rustc_infer::traits::{ObligationCauseCode, ObligationInspector, WellFormedLoc};
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::config;
use rustc_span::Span;
Expand Down Expand Up @@ -247,6 +247,13 @@ fn typeck_with_inspect<'tcx>(

debug!(pending_obligations = ?fcx.fulfillment_cx.borrow().pending_obligations());

// We need to handle opaque types before emitting ambiguity errors as applying
// defining uses may guide type inference.
if fcx.next_trait_solver() {
fcx.handle_opaque_type_uses_next();
}

fcx.select_obligations_where_possible(|_| {});
if let None = fcx.infcx.tainted_by_errors() {
fcx.report_ambiguity_errors();
}
Expand All @@ -259,21 +266,6 @@ fn typeck_with_inspect<'tcx>(

let typeck_results = fcx.resolve_type_vars_in_body(body);

// Handle potentially region dependent goals, see `InferCtxt::in_hir_typeck`.
if let None = fcx.infcx.tainted_by_errors() {
for obligation in fcx.take_hir_typeck_potentially_region_dependent_goals() {
let obligation = fcx.resolve_vars_if_possible(obligation);
if obligation.has_non_region_infer() {
bug!("unexpected inference variable after writeback: {obligation:?}");
}
fcx.register_predicate(obligation);
}
fcx.select_obligations_where_possible(|_| {});
if let None = fcx.infcx.tainted_by_errors() {
fcx.report_ambiguity_errors();
}
}

fcx.detect_opaque_types_added_during_writeback();

// Consistency check our TypeckResults instance can hold all ItemLocalIds
Expand Down
Loading
Loading