[ty] Emit error for invalid binary operations in type expressions (#18991)

## Summary

This PR adds diagnostic for invalid binary operators in type
expressions. It should close https://github.com/astral-sh/ty/issues/706
if merged.

Please feel free to suggest better wordings for the diagnostic message.

## Test Plan

I modified `mdtest/annotations/invalid.md` and added a test for each
binary operator, and fixed tests that was broken by the new diagnostic.
This commit is contained in:
med1844 2025-06-30 01:06:01 -07:00 committed by GitHub
parent 9469a982cc
commit 0ec2ad2fa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 2 deletions

View file

@ -8416,8 +8416,17 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
UnionType::from_elements(self.db(), [left_ty, right_ty])
}
// anything else is an invalid annotation:
_ => {
op => {
self.infer_binary_expression(binary);
if let Some(mut diag) = self.report_invalid_type_expression(
expression,
format_args!(
"Invalid binary operator `{}` in type annotation",
op.as_str()
),
) {
diag.info("Did you mean to use `|`?");
}
Type::unknown()
}
}