Skip to content

Commit d0b7393

Browse files
More eagerly bail in DispatchFromDyn validation
1 parent ed49386 commit d0b7393

File tree

1 file changed

+22
-25
lines changed
  • compiler/rustc_hir_analysis/src/coherence

1 file changed

+22
-25
lines changed

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,16 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
252252
}));
253253
}
254254

255-
let mut res = Ok(());
256255
if def_a.repr().c() || def_a.repr().packed() {
257-
res = Err(tcx.dcx().emit_err(errors::DispatchFromDynRepr { span }));
256+
return Err(tcx.dcx().emit_err(errors::DispatchFromDynRepr { span }));
258257
}
259258

260259
let fields = &def_a.non_enum_variant().fields;
261260

261+
let mut res = Ok(());
262262
let coerced_fields = fields
263-
.iter()
264-
.filter(|field| {
263+
.iter_enumerated()
264+
.filter_map(|(i, field)| {
265265
// Ignore PhantomData fields
266266
let unnormalized_ty = tcx.type_of(field.did).instantiate_identity();
267267
if tcx
@@ -272,7 +272,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
272272
.unwrap_or(unnormalized_ty)
273273
.is_phantom_data()
274274
{
275-
return false;
275+
return None;
276276
}
277277

278278
let ty_a = field.ty(tcx, args_a);
@@ -290,7 +290,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
290290
&& !ty_a.has_non_region_param()
291291
{
292292
// ignore 1-ZST fields
293-
return false;
293+
return None;
294294
}
295295

296296
res = Err(tcx.dcx().emit_err(errors::DispatchFromDynZST {
@@ -299,60 +299,57 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
299299
ty: ty_a,
300300
}));
301301

302-
return false;
302+
None
303+
} else {
304+
Some((i, ty_a, ty_b))
303305
}
304-
305-
true
306306
})
307307
.collect::<Vec<_>>();
308+
res?;
308309

309310
if coerced_fields.is_empty() {
310-
res = Err(tcx.dcx().emit_err(errors::DispatchFromDynSingle {
311+
return Err(tcx.dcx().emit_err(errors::DispatchFromDynSingle {
311312
span,
312313
trait_name: "DispatchFromDyn",
313314
note: true,
314315
}));
315316
} else if coerced_fields.len() > 1 {
316-
res = Err(tcx.dcx().emit_err(errors::DispatchFromDynMulti {
317+
return Err(tcx.dcx().emit_err(errors::DispatchFromDynMulti {
317318
span,
318319
coercions_note: true,
319320
number: coerced_fields.len(),
320321
coercions: coerced_fields
321322
.iter()
322-
.map(|field| {
323-
format!(
324-
"`{}` (`{}` to `{}`)",
325-
field.name,
326-
field.ty(tcx, args_a),
327-
field.ty(tcx, args_b),
328-
)
323+
.map(|&(i, ty_a, ty_b)| {
324+
format!("`{}` (`{}` to `{}`)", fields[i].name, ty_a, ty_b,)
329325
})
330326
.collect::<Vec<_>>()
331327
.join(", "),
332328
}));
333329
} else {
334330
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
335-
for field in coerced_fields {
331+
for (_, ty_a, ty_b) in coerced_fields {
336332
ocx.register_obligation(Obligation::new(
337333
tcx,
338334
cause.clone(),
339335
param_env,
340336
ty::TraitRef::new(
341337
tcx,
342338
dispatch_from_dyn_trait,
343-
[field.ty(tcx, args_a), field.ty(tcx, args_b)],
339+
[ty_a, ty_b],
344340
),
345341
));
346342
}
347343
let errors = ocx.select_all_or_error();
348344
if !errors.is_empty() {
349-
res = Err(infcx.err_ctxt().report_fulfillment_errors(errors));
345+
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
350346
}
351347

352348
// Finally, resolve all regions.
353-
res = res.and(ocx.resolve_regions_and_report_errors(impl_did, param_env, []));
349+
ocx.resolve_regions_and_report_errors(impl_did, param_env, [])?;
354350
}
355-
res
351+
352+
Ok(())
356353
}
357354
_ => Err(tcx
358355
.dcx()
@@ -558,11 +555,11 @@ pub(crate) fn coerce_unsized_info<'tcx>(
558555
ocx.register_obligation(obligation);
559556
let errors = ocx.select_all_or_error();
560557
if !errors.is_empty() {
561-
infcx.err_ctxt().report_fulfillment_errors(errors);
558+
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
562559
}
563560

564561
// Finally, resolve all regions.
565-
let _ = ocx.resolve_regions_and_report_errors(impl_did, param_env, []);
562+
ocx.resolve_regions_and_report_errors(impl_did, param_env, [])?;
566563

567564
Ok(CoerceUnsizedInfo { custom_kind: kind })
568565
}

0 commit comments

Comments
 (0)