Edit operator type errors

This commit is contained in:
Ian Manske 2024-11-23 17:49:26 -08:00
parent 4d3283e235
commit caaa2472ec
4 changed files with 746 additions and 979 deletions

View file

@ -156,13 +156,13 @@ pub(crate) fn compile_expression(
Ok(()) Ok(())
} }
Expr::BinaryOp(lhs, op, rhs) => { Expr::BinaryOp(lhs, op, rhs) => {
if let Expr::Operator(ref operator) = op.expr { if let Expr::Operator(operator) = op.expr {
drop_input(builder)?; drop_input(builder)?;
compile_binary_op( compile_binary_op(
working_set, working_set,
builder, builder,
lhs, lhs,
operator.clone().into_spanned(op.span), operator.into_spanned(op.span),
rhs, rhs,
expr.span, expr.span,
out_reg, out_reg,

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,7 @@ use std::fmt::Display;
use super::{Expr, Expression}; use super::{Expr, Expression};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Comparison { pub enum Comparison {
Equal, Equal,
NotEqual, NotEqual,
@ -21,7 +21,7 @@ pub enum Comparison {
EndsWith, EndsWith,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Math { pub enum Math {
Plus, Plus,
Concat, Concat,
@ -33,14 +33,14 @@ pub enum Math {
Pow, Pow,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Boolean { pub enum Boolean {
And, And,
Or, Or,
Xor, Xor,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Bits { pub enum Bits {
BitOr, BitOr,
BitXor, BitXor,
@ -49,7 +49,7 @@ pub enum Bits {
ShiftRight, ShiftRight,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Assignment { pub enum Assignment {
Assign, Assign,
PlusAssign, PlusAssign,
@ -59,7 +59,7 @@ pub enum Assignment {
DivideAssign, DivideAssign,
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Operator { pub enum Operator {
Comparison(Comparison), Comparison(Comparison),
Math(Math), Math(Math),
@ -170,7 +170,7 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
Expression { Expression {
expr: Expr::Operator(operator), expr: Expr::Operator(operator),
.. ..
} => Ok(operator.clone()), } => Ok(*operator),
Expression { span, expr, .. } => Err(ShellError::UnknownOperator { Expression { span, expr, .. } => Err(ShellError::UnknownOperator {
op_token: format!("{expr:?}"), op_token: format!("{expr:?}"),
span: *span, span: *span,

View file

@ -111,25 +111,36 @@ pub enum ParseError {
span: Span, span: Span,
}, },
#[error("{0} is not supported on values of type {3}")] /// One or more of the values have types not supported by the operator.
#[diagnostic(code(nu::parser::unsupported_operation))] #[error("{op} is not supported on values of type {unsupported}.")]
UnsupportedOperationLHS( #[diagnostic(code(nu::parser::operator_unsupported_type))]
String, OperatorUnsupportedType {
#[label = "doesn't support this value"] Span, op: &'static str,
#[label("{3}")] Span, unsupported: Type,
Type, #[label = "does support this type"]
), op_span: Span,
#[label("{unsupported}")]
unsupported_span: Span,
#[help]
help: Option<&'static str>,
},
#[error("{0} is not supported between {3} and {5}.")] /// The operator supports the types of both values, but not the specific combination of their types.
#[diagnostic(code(nu::parser::unsupported_operation))] #[error("{op} is not supported between types {lhs} and {rhs}.")]
UnsupportedOperationRHS( #[diagnostic(code(nu::parser::operator_type_mismatch))]
String, OperatorTypeMismatch {
#[label = "doesn't support these values"] Span, op: &'static str,
#[label("{3}")] Span, lhs: Type,
Type, rhs: Type,
#[label("{5}")] Span, #[label = "does not operate between these two types"]
Type, op_span: Span,
), #[label("{lhs}")]
lhs_span: Span,
#[label("{rhs}")]
rhs_span: Span,
#[help]
help: Option<&'static str>,
},
#[error("{0} is not supported between {3}, {5}, and {7}.")] #[error("{0} is not supported between {3}, {5}, and {7}.")]
#[diagnostic(code(nu::parser::unsupported_operation))] #[diagnostic(code(nu::parser::unsupported_operation))]
@ -528,8 +539,8 @@ impl ParseError {
ParseError::Expected(_, s) => *s, ParseError::Expected(_, s) => *s,
ParseError::ExpectedWithStringMsg(_, s) => *s, ParseError::ExpectedWithStringMsg(_, s) => *s,
ParseError::Mismatch(_, _, s) => *s, ParseError::Mismatch(_, _, s) => *s,
ParseError::UnsupportedOperationLHS(_, _, s, _) => *s, ParseError::OperatorUnsupportedType { op_span, .. } => *op_span,
ParseError::UnsupportedOperationRHS(_, _, _, _, s, _) => *s, ParseError::OperatorTypeMismatch { op_span, .. } => *op_span,
ParseError::UnsupportedOperationTernary(_, _, _, _, _, _, s, _) => *s, ParseError::UnsupportedOperationTernary(_, _, _, _, _, _, s, _) => *s,
ParseError::ExpectedKeyword(_, s) => *s, ParseError::ExpectedKeyword(_, s) => *s,
ParseError::UnexpectedKeyword(_, s) => *s, ParseError::UnexpectedKeyword(_, s) => *s,