[WIP] Don't rely on the type of lhs
when type checking lhs OP rhs
#19434
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Tweaks the binary operator dispatch algorithm to not rely on the full resolution of the type of the LHS.
Closes #8280
Closes #19035
This is an overview of the current type checking algorithm vs the new algorithm introduced in this PR:
Current
lhs
check_expr(lhs)
)lhs
(compiler error if can't fully resolve)lhs_ty = structurally_resolved_type(lhs)
)lhs_ty
is integer andOP
is bit-shiftrhs_ty
is a subtype ofuint
lhs_ty
andOP
, iflhs_ty OP lhs_ty
is a built-in operationis_binopable(lhs_ty, op)
returns true)T op 1.0
works, but1.0 op T
doesn't #19035rhs_ty
a subtype oflhs_ty
and "write" the type oflhs OP rhs
expression&&
or||
New (this PR)
lhs
andrhs
check_expr(lhs|rhs)
)lhs
andrhs
try_structurally_resolved_type(lhs|rhs)
returnSome
)lhs_ty
andrhs_ty
are integers andOP
is bit-shiftrhs_ty
is a subtype ofuint
lhs_ty
,rhs_ty
andOP
, iflhs_ty
andrhs_ty
have the same category andlhs OP rhs
is a built-in operationis_builtin_binop(cx, lhs_ty, op)
returns true)lhs OP rhs
expression&&
or||
This PR passes
make check
as it is, but it comes with two new issues:&'a T == &'b T
work even though only&'a T == &'a T
(note same lifetime'a
) should work ... well, it no longer works (or it doesn't work in most cases). However, this is not a real problem, because with Overloaded comparison traits #19167 (overloaded==
operator), we can nowimpl PartialEq<&'b T> for &'a T
to take care references of with different lifetimes.Which now generates the following error message:
==
cannot be applied to typeFoo
" (and its!=
brother) should appear only once.==
cannot be applied to typeFoo
", although it sounds absurd, it's actually a real error, just that the message it's bad. That message it's actually referring to the operationbool && [Type Error]
where[Type Error]
is the type ofFoo == Foo
(which can't be resolved, sinceFoo
doesn't implementPartialEq
).@nikomatsakis Thoughts? I think I can fix/improve the error messages, but I'd like to know what you think about the approach and whether the "reborrow adjustment" can/should be fixed or not.