Skip to content

Commit

Permalink
Clean up dropck code a bit
Browse files Browse the repository at this point in the history
- Remove `Result` that couldn't be Err on valid compilation.
- Always compute errors on failure.
  • Loading branch information
matthewjasper committed Feb 17, 2025
1 parent 87e5969 commit 49cf00c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 55 deletions.
24 changes: 12 additions & 12 deletions compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_index::interval::IntervalSet;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::for_liveness;
use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, HasLocalDecls, Local, Location};
use rustc_middle::span_bug;
use rustc_middle::traits::query::DropckOutlivesResult;
use rustc_middle::ty::relate::Relate;
use rustc_middle::ty::{Ty, TyCtxt, TypeVisitable, TypeVisitableExt};
Expand Down Expand Up @@ -613,23 +614,22 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
// can't rely on the the `ErrorGuaranteed` from `fully_perform` here
// because it comes from delay_span_bug.
let ocx = ObligationCtxt::new_with_diagnostics(&typeck.infcx);
let errors = match dropck_outlives::compute_dropck_outlives_with_errors(
&ocx, op, span, true,
) {
Ok(_) => ocx.select_all_or_error(),
Err(e) => {
if e.is_empty() {
ocx.select_all_or_error()
} else {
e
let errors =
match dropck_outlives::compute_dropck_outlives_with_errors(&ocx, op, span) {
Ok(_) => ocx.select_all_or_error(),
Err(e) => {
if e.is_empty() {
ocx.select_all_or_error()
} else {
e
}
}
}
};
};

if !errors.is_empty() {
typeck.infcx.err_ctxt().report_fulfillment_errors(errors);
} else {
rustc_middle::span_bug!(span, "Rerunning drop data query produced no error.");
span_bug!(span, "Rerunning drop data query produced no error.");
}
DropData { dropck_result: Default::default(), region_constraint_data: None }
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ rustc_queries! {

query adt_dtorck_constraint(
key: DefId
) -> Result<&'tcx DropckConstraint<'tcx>, NoSolution> {
) -> &'tcx DropckConstraint<'tcx> {
desc { |tcx| "computing drop-check constraints for `{}`", tcx.def_path_str(key) }
}

Expand Down
56 changes: 21 additions & 35 deletions compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn compute_dropck_outlives_inner<'tcx>(
goal: ParamEnvAnd<'tcx, DropckOutlives<'tcx>>,
span: Span,
) -> Result<DropckOutlivesResult<'tcx>, NoSolution> {
match compute_dropck_outlives_with_errors(ocx, goal, span, false) {
match compute_dropck_outlives_with_errors(ocx, goal, span) {
Ok(r) => Ok(r),
Err(_) => Err(NoSolution),
}
Expand All @@ -104,7 +104,6 @@ pub fn compute_dropck_outlives_with_errors<'tcx, E>(
ocx: &ObligationCtxt<'_, 'tcx, E>,
goal: ParamEnvAnd<'tcx, DropckOutlives<'tcx>>,
span: Span,
report_errors: bool,
) -> Result<DropckOutlivesResult<'tcx>, Vec<E>>
where
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
Expand Down Expand Up @@ -162,17 +161,14 @@ where
result.overflows.len(),
ty_stack.len()
);
match dtorck_constraint_for_ty_inner(
dtorck_constraint_for_ty_inner(
tcx,
ocx.infcx.typing_env(param_env),
span,
depth,
ty,
&mut constraints,
) {
Err(_) => return Err(Vec::new()),
_ => (),
};
);

// "outlives" represent types/regions that may be touched
// by a destructor.
Expand All @@ -192,24 +188,19 @@ where
// do not themselves define a destructor", more or less. We have
// to push them onto the stack to be expanded.
for ty in constraints.dtorck_types.drain(..) {
let ty = if report_errors {
let normalized_ty = ocx.deeply_normalize(&cause, param_env, ty)?;

let errors = ocx.select_where_possible();
if !errors.is_empty() {
debug!("failed to normalize dtorck type: {ty} ~> {errors:#?}");
return Err(errors);
}
normalized_ty
} else if let Ok(Normalized { value: ty, obligations }) =
let ty = if let Ok(Normalized { value: ty, obligations }) =
ocx.infcx.at(&cause, param_env).query_normalize(ty)
{
ocx.register_obligations(obligations);

debug!("dropck_outlives: ty from dtorck_types = {:?}", ty);
ty
} else {
return Err(Vec::new());
ocx.deeply_normalize(&cause, param_env, ty)?;

let errors = ocx.select_where_possible();
debug!("normalize errors: {ty} ~> {errors:#?}");
return Err(errors);
};

match ty.kind() {
Expand Down Expand Up @@ -246,14 +237,14 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
depth: usize,
ty: Ty<'tcx>,
constraints: &mut DropckConstraint<'tcx>,
) -> Result<(), NoSolution> {
) {
if !tcx.recursion_limit().value_within_limit(depth) {
constraints.overflows.push(ty);
return Ok(());
return;
}

if trivial_dropck_outlives(tcx, ty) {
return Ok(());
return;
}

match ty.kind() {
Expand All @@ -277,22 +268,20 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
// single-element containers, behave like their element
rustc_data_structures::stack::ensure_sufficient_stack(|| {
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, *ety, constraints)
})?;
});
}

ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in tys.iter() {
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints)?;
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints);
}
Ok::<_, NoSolution>(())
})?,
}),

ty::Closure(_, args) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in args.as_closure().upvar_tys() {
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints)?;
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints);
}
Ok::<_, NoSolution>(())
})?,
}),

ty::CoroutineClosure(_, args) => {
rustc_data_structures::stack::ensure_sufficient_stack(|| {
Expand All @@ -304,10 +293,9 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
depth + 1,
ty,
constraints,
)?;
);
}
Ok::<_, NoSolution>(())
})?
})
}

ty::Coroutine(_, args) => {
Expand Down Expand Up @@ -346,7 +334,7 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(

ty::Adt(def, args) => {
let DropckConstraint { dtorck_types, outlives, overflows } =
tcx.at(span).adt_dtorck_constraint(def.did())?;
tcx.at(span).adt_dtorck_constraint(def.did());
// FIXME: we can try to recursively `dtorck_constraint_on_ty`
// there, but that needs some way to handle cycles.
constraints
Expand Down Expand Up @@ -379,9 +367,7 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(..) | ty::Error(_) => {
// By the time this code runs, all type variables ought to
// be fully resolved.
return Err(NoSolution);
tcx.dcx().span_delayed_bug(span, format!("Unresolved type in dropck: {:?}.", ty));
}
}

Ok(())
}
11 changes: 4 additions & 7 deletions compiler/rustc_traits/src/dropck_outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ fn dropck_outlives<'tcx>(
}

/// Calculates the dtorck constraint for a type.
pub(crate) fn adt_dtorck_constraint(
tcx: TyCtxt<'_>,
def_id: DefId,
) -> Result<&DropckConstraint<'_>, NoSolution> {
pub(crate) fn adt_dtorck_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &DropckConstraint<'_> {
let def = tcx.adt_def(def_id);
let span = tcx.def_span(def_id);
let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
Expand All @@ -52,20 +49,20 @@ pub(crate) fn adt_dtorck_constraint(
overflows: vec![],
};
debug!("dtorck_constraint: {:?} => {:?}", def, result);
return Ok(tcx.arena.alloc(result));
return tcx.arena.alloc(result);
}

let mut result = DropckConstraint::empty();
for field in def.all_fields() {
let fty = tcx.type_of(field.did).instantiate_identity();
dtorck_constraint_for_ty_inner(tcx, typing_env, span, 0, fty, &mut result)?;
dtorck_constraint_for_ty_inner(tcx, typing_env, span, 0, fty, &mut result);
}
result.outlives.extend(tcx.destructor_constraints(def));
dedup_dtorck_constraint(&mut result);

debug!("dtorck_constraint: {:?} => {:?}", def, result);

Ok(tcx.arena.alloc(result))
tcx.arena.alloc(result)
}

fn dedup_dtorck_constraint(c: &mut DropckConstraint<'_>) {
Expand Down

0 comments on commit 49cf00c

Please sign in to comment.