Skip to content

Commit 485ae9f

Browse files
committed
Always check predicates in can_coerce
This only changed two tests and I consider both changes an improvement.
1 parent 5c14433 commit 485ae9f

File tree

4 files changed

+11
-27
lines changed

4 files changed

+11
-27
lines changed

compiler/rustc_typeck/src/check/coercion.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -934,20 +934,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
934934
}
935935

936936
/// Same as `try_coerce()`, but without side-effects.
937+
///
938+
/// Returns false if the coercion creates any obligations that result in
939+
/// errors.
937940
pub fn can_coerce(&self, expr_ty: Ty<'tcx>, target: Ty<'tcx>) -> bool {
938-
let source = self.resolve_vars_with_obligations(expr_ty);
939-
debug!("coercion::can({:?} -> {:?})", source, target);
940-
941-
let cause = self.cause(rustc_span::DUMMY_SP, ObligationCauseCode::ExprAssignable);
942-
// We don't ever need two-phase here since we throw out the result of the coercion
943-
let coerce = Coerce::new(self, cause, AllowTwoPhase::No);
944-
self.probe(|_| coerce.coerce(source, target)).is_ok()
945-
}
946-
947-
/// Same as `try_coerce()`, but without side-effects and attempts to select
948-
/// all predicates created by the coercion. This is useful for e.g. checking
949-
/// that associated types are correct.
950-
pub fn can_coerce_and_satisfy_predicates(&self, expr_ty: Ty<'tcx>, target: Ty<'tcx>) -> bool {
951941
let source = self.resolve_vars_with_obligations(expr_ty);
952942
debug!("coercion::can_with_predicates({:?} -> {:?})", source, target);
953943

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
373373
let box_found = self.tcx.mk_box(found);
374374
let pin_box_found = self.tcx.mk_lang_item(box_found, LangItem::Pin).unwrap();
375375
let pin_found = self.tcx.mk_lang_item(found, LangItem::Pin).unwrap();
376-
if self.can_coerce_and_satisfy_predicates(pin_box_found, expected) {
376+
if self.can_coerce(pin_box_found, expected) {
377377
debug!("can coerce {:?} to {:?}, suggesting Box::pin", pin_box_found, expected);
378378
match found.kind() {
379379
ty::Adt(def, _) if def.is_box() => {
@@ -391,7 +391,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
391391
}
392392
}
393393
true
394-
} else if self.can_coerce_and_satisfy_predicates(pin_found, expected) {
394+
} else if self.can_coerce(pin_found, expected) {
395395
match found.kind() {
396396
ty::Adt(def, _) if def.is_box() => {
397397
err.help("use `Box::pin`");

src/test/ui/cross/cross-borrow-trait.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0308]: mismatched types
22
--> $DIR/cross-borrow-trait.rs:10:26
33
|
44
LL | let _y: &dyn Trait = x;
5-
| ---------- ^
6-
| | |
7-
| | expected `&dyn Trait`, found struct `Box`
8-
| | help: consider borrowing here: `&x`
5+
| ---------- ^ expected `&dyn Trait`, found struct `Box`
6+
| |
97
| expected due to this
108
|
119
= note: expected reference `&dyn Trait`

src/test/ui/dst/dst-bad-coercions.stderr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ error[E0308]: mismatched types
1313
--> $DIR/dst-bad-coercions.rs:15:21
1414
|
1515
LL | let y: &dyn T = x;
16-
| ------ ^
17-
| | |
18-
| | expected `&dyn T`, found *-ptr
19-
| | help: consider borrowing here: `&x`
16+
| ------ ^ expected `&dyn T`, found *-ptr
17+
| |
2018
| expected due to this
2119
|
2220
= note: expected reference `&dyn T`
@@ -37,10 +35,8 @@ error[E0308]: mismatched types
3735
--> $DIR/dst-bad-coercions.rs:20:21
3836
|
3937
LL | let y: &dyn T = x;
40-
| ------ ^
41-
| | |
42-
| | expected `&dyn T`, found *-ptr
43-
| | help: consider borrowing here: `&x`
38+
| ------ ^ expected `&dyn T`, found *-ptr
39+
| |
4440
| expected due to this
4541
|
4642
= note: expected reference `&dyn T`

0 commit comments

Comments
 (0)