Skip to content

Commit 49cf00c

Browse files
committed
Clean up dropck code a bit
- Remove `Result` that couldn't be Err on valid compilation. - Always compute errors on failure.
1 parent 87e5969 commit 49cf00c

File tree

4 files changed

+38
-55
lines changed

4 files changed

+38
-55
lines changed

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_index::interval::IntervalSet;
44
use rustc_infer::infer::canonical::QueryRegionConstraints;
55
use rustc_infer::infer::outlives::for_liveness;
66
use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, HasLocalDecls, Local, Location};
7+
use rustc_middle::span_bug;
78
use rustc_middle::traits::query::DropckOutlivesResult;
89
use rustc_middle::ty::relate::Relate;
910
use rustc_middle::ty::{Ty, TyCtxt, TypeVisitable, TypeVisitableExt};
@@ -613,23 +614,22 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
613614
// can't rely on the the `ErrorGuaranteed` from `fully_perform` here
614615
// because it comes from delay_span_bug.
615616
let ocx = ObligationCtxt::new_with_diagnostics(&typeck.infcx);
616-
let errors = match dropck_outlives::compute_dropck_outlives_with_errors(
617-
&ocx, op, span, true,
618-
) {
619-
Ok(_) => ocx.select_all_or_error(),
620-
Err(e) => {
621-
if e.is_empty() {
622-
ocx.select_all_or_error()
623-
} else {
624-
e
617+
let errors =
618+
match dropck_outlives::compute_dropck_outlives_with_errors(&ocx, op, span) {
619+
Ok(_) => ocx.select_all_or_error(),
620+
Err(e) => {
621+
if e.is_empty() {
622+
ocx.select_all_or_error()
623+
} else {
624+
e
625+
}
625626
}
626-
}
627-
};
627+
};
628628

629629
if !errors.is_empty() {
630630
typeck.infcx.err_ctxt().report_fulfillment_errors(errors);
631631
} else {
632-
rustc_middle::span_bug!(span, "Rerunning drop data query produced no error.");
632+
span_bug!(span, "Rerunning drop data query produced no error.");
633633
}
634634
DropData { dropck_result: Default::default(), region_constraint_data: None }
635635
}

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ rustc_queries! {
802802

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

compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs

+21-35
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn compute_dropck_outlives_inner<'tcx>(
9494
goal: ParamEnvAnd<'tcx, DropckOutlives<'tcx>>,
9595
span: Span,
9696
) -> Result<DropckOutlivesResult<'tcx>, NoSolution> {
97-
match compute_dropck_outlives_with_errors(ocx, goal, span, false) {
97+
match compute_dropck_outlives_with_errors(ocx, goal, span) {
9898
Ok(r) => Ok(r),
9999
Err(_) => Err(NoSolution),
100100
}
@@ -104,7 +104,6 @@ pub fn compute_dropck_outlives_with_errors<'tcx, E>(
104104
ocx: &ObligationCtxt<'_, 'tcx, E>,
105105
goal: ParamEnvAnd<'tcx, DropckOutlives<'tcx>>,
106106
span: Span,
107-
report_errors: bool,
108107
) -> Result<DropckOutlivesResult<'tcx>, Vec<E>>
109108
where
110109
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
@@ -162,17 +161,14 @@ where
162161
result.overflows.len(),
163162
ty_stack.len()
164163
);
165-
match dtorck_constraint_for_ty_inner(
164+
dtorck_constraint_for_ty_inner(
166165
tcx,
167166
ocx.infcx.typing_env(param_env),
168167
span,
169168
depth,
170169
ty,
171170
&mut constraints,
172-
) {
173-
Err(_) => return Err(Vec::new()),
174-
_ => (),
175-
};
171+
);
176172

177173
// "outlives" represent types/regions that may be touched
178174
// by a destructor.
@@ -192,24 +188,19 @@ where
192188
// do not themselves define a destructor", more or less. We have
193189
// to push them onto the stack to be expanded.
194190
for ty in constraints.dtorck_types.drain(..) {
195-
let ty = if report_errors {
196-
let normalized_ty = ocx.deeply_normalize(&cause, param_env, ty)?;
197-
198-
let errors = ocx.select_where_possible();
199-
if !errors.is_empty() {
200-
debug!("failed to normalize dtorck type: {ty} ~> {errors:#?}");
201-
return Err(errors);
202-
}
203-
normalized_ty
204-
} else if let Ok(Normalized { value: ty, obligations }) =
191+
let ty = if let Ok(Normalized { value: ty, obligations }) =
205192
ocx.infcx.at(&cause, param_env).query_normalize(ty)
206193
{
207194
ocx.register_obligations(obligations);
208195

209196
debug!("dropck_outlives: ty from dtorck_types = {:?}", ty);
210197
ty
211198
} else {
212-
return Err(Vec::new());
199+
ocx.deeply_normalize(&cause, param_env, ty)?;
200+
201+
let errors = ocx.select_where_possible();
202+
debug!("normalize errors: {ty} ~> {errors:#?}");
203+
return Err(errors);
213204
};
214205

215206
match ty.kind() {
@@ -246,14 +237,14 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
246237
depth: usize,
247238
ty: Ty<'tcx>,
248239
constraints: &mut DropckConstraint<'tcx>,
249-
) -> Result<(), NoSolution> {
240+
) {
250241
if !tcx.recursion_limit().value_within_limit(depth) {
251242
constraints.overflows.push(ty);
252-
return Ok(());
243+
return;
253244
}
254245

255246
if trivial_dropck_outlives(tcx, ty) {
256-
return Ok(());
247+
return;
257248
}
258249

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

283274
ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
284275
for ty in tys.iter() {
285-
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints)?;
276+
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints);
286277
}
287-
Ok::<_, NoSolution>(())
288-
})?,
278+
}),
289279

290280
ty::Closure(_, args) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
291281
for ty in args.as_closure().upvar_tys() {
292-
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints)?;
282+
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints);
293283
}
294-
Ok::<_, NoSolution>(())
295-
})?,
284+
}),
296285

297286
ty::CoroutineClosure(_, args) => {
298287
rustc_data_structures::stack::ensure_sufficient_stack(|| {
@@ -304,10 +293,9 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
304293
depth + 1,
305294
ty,
306295
constraints,
307-
)?;
296+
);
308297
}
309-
Ok::<_, NoSolution>(())
310-
})?
298+
})
311299
}
312300

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

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

compiler/rustc_traits/src/dropck_outlives.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ fn dropck_outlives<'tcx>(
3030
}
3131

3232
/// Calculates the dtorck constraint for a type.
33-
pub(crate) fn adt_dtorck_constraint(
34-
tcx: TyCtxt<'_>,
35-
def_id: DefId,
36-
) -> Result<&DropckConstraint<'_>, NoSolution> {
33+
pub(crate) fn adt_dtorck_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &DropckConstraint<'_> {
3734
let def = tcx.adt_def(def_id);
3835
let span = tcx.def_span(def_id);
3936
let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
@@ -52,20 +49,20 @@ pub(crate) fn adt_dtorck_constraint(
5249
overflows: vec![],
5350
};
5451
debug!("dtorck_constraint: {:?} => {:?}", def, result);
55-
return Ok(tcx.arena.alloc(result));
52+
return tcx.arena.alloc(result);
5653
}
5754

5855
let mut result = DropckConstraint::empty();
5956
for field in def.all_fields() {
6057
let fty = tcx.type_of(field.did).instantiate_identity();
61-
dtorck_constraint_for_ty_inner(tcx, typing_env, span, 0, fty, &mut result)?;
58+
dtorck_constraint_for_ty_inner(tcx, typing_env, span, 0, fty, &mut result);
6259
}
6360
result.outlives.extend(tcx.destructor_constraints(def));
6461
dedup_dtorck_constraint(&mut result);
6562

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

68-
Ok(tcx.arena.alloc(result))
65+
tcx.arena.alloc(result)
6966
}
7067

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

0 commit comments

Comments
 (0)