Skip to content

Commit

Permalink
Always structurally resolve coercion target
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 10, 2024
1 parent a1eceec commit 39b5936
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
12 changes: 5 additions & 7 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,17 +1051,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
expr: &'tcx hir::Expr<'tcx>,
expr_ty: Ty<'tcx>,
mut target: Ty<'tcx>,
target: Ty<'tcx>,
allow_two_phase: AllowTwoPhase,
cause: Option<ObligationCause<'tcx>>,
) -> RelateResult<'tcx, Ty<'tcx>> {
let source = self.try_structurally_resolve_type(expr.span, expr_ty);
if self.next_trait_solver() {
target = self.try_structurally_resolve_type(
cause.as_ref().map_or(expr.span, |cause| cause.span),
target,
);
}
let target = self.try_structurally_resolve_type(
cause.as_ref().map_or(expr.span, |cause| cause.span),
target,
);
debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);

let cause =
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&'a self,
mut expr: &'tcx hir::Expr<'tcx>,
checked_ty: Ty<'tcx>,
expected: Ty<'tcx>,
expected_ty: Ty<'tcx>,
mut expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
allow_two_phase: AllowTwoPhase,
) -> Result<Ty<'tcx>, Diag<'a>> {
let expected = self.resolve_vars_with_obligations(expected);

let e = match self.coerce(expr, checked_ty, expected, allow_two_phase, None) {
let e = match self.coerce(expr, checked_ty, expected_ty, allow_two_phase, None) {
Ok(ty) => return Ok(ty),
Err(e) => e,
};
Expand All @@ -271,6 +269,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let expr = expr.peel_drop_temps();
let cause = self.misc(expr.span);
let expr_ty = self.resolve_vars_if_possible(checked_ty);
let expected = self.resolve_vars_if_possible(expected_ty);
let mut err = self.err_ctxt().report_mismatched_types(&cause, expected, expr_ty, e);

self.emit_coerce_suggestions(&mut err, expr, expr_ty, expected, expected_ty_expr, Some(e));
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// `ExpectHasType(expected_ty)`, or the `formal_ty` otherwise.
let coerced_ty = expectation.only_has_type(self).unwrap_or(formal_input_ty);

// Cause selection errors caused by resolving a single argument to point at the
// argument and not the call. This lets us customize the span pointed to in the
// fulfillment error to be more accurate.
let coerced_ty = self.resolve_vars_with_obligations(coerced_ty);

let coerce_error =
self.coerce(provided_arg, checked_ty, coerced_ty, AllowTwoPhase::Yes, None).err();
if coerce_error.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// see `NB` above
let rhs_ty = self.check_expr_coercible_to_type(rhs_expr, rhs_ty_var, Some(lhs_expr));
let rhs_ty = self.resolve_vars_with_obligations(rhs_ty);
let rhs_ty = self.resolve_vars_if_possible(rhs_ty);

let return_ty = match result {
Ok(method) => {
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/offset-of/offset-of-dst-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ LL | offset_of!((u8, dyn Trait), 1);
= help: the trait `Sized` is not implemented for `dyn Trait`
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:44:5
|
LL | offset_of!(Delta<Alpha>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `Alpha`, the trait `Sized` is not implemented for `[u8]`, which is required by `Alpha: Sized`
note: required because it appears within the type `Alpha`
--> $DIR/offset-of-dst-field.rs:5:8
|
LL | struct Alpha {
| ^^^^^
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `Extern` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:45:5
|
Expand All @@ -52,20 +66,6 @@ LL | offset_of!(Delta<dyn Trait>, z);
= help: the trait `Sized` is not implemented for `dyn Trait`
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:44:5
|
LL | offset_of!(Delta<Alpha>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `Alpha`, the trait `Sized` is not implemented for `[u8]`, which is required by `Alpha: Sized`
note: required because it appears within the type `Alpha`
--> $DIR/offset-of-dst-field.rs:5:8
|
LL | struct Alpha {
| ^^^^^
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:50:5
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ error[E0308]: mismatched types
--> $DIR/lazy_subtyping_of_opaques.rs:11:5
|
LL | Thunk::new(|cont| cont)
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `Thunk<_>`, found `()`
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `Thunk<()>`, found `()`
|
= note: expected struct `Thunk<_>`
= note: expected struct `Thunk<()>`
found unit type `()`

error: aborting due to 3 previous errors
Expand Down

0 comments on commit 39b5936

Please sign in to comment.