compiler: Warn user when immediately converting gradient macro to color (#6956)

Fixes #6819
This commit is contained in:
crai0 2024-12-02 12:05:38 +00:00 committed by GitHub
parent 35e094e723
commit e4b70efe6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View file

@ -1107,6 +1107,14 @@ impl Expression {
self
} else if ty.can_convert(&target_type) {
let from = match (ty, &target_type) {
(Type::Brush, Type::Color) => match self {
Expression::LinearGradient { .. } | Expression::RadialGradient { .. } => {
let message = format!("Narrowing conversion from {0} to {1}. This can lead to unexpected behavior because the {0} is a gradient", Type::Brush, Type::Color);
diag.push_warning(message, node);
self
}
_ => self,
},
(Type::Percent, Type::Float32) => Expression::BinaryExpression {
lhs: Box::new(self),
rhs: Box::new(Expression::NumberLiteral(0.01, Unit::None)),

View file

@ -29,4 +29,6 @@ export X := Rectangle {
// ^error{Unknown unqualified identifier 'r'}
property <brush> g15: @linear-gradient(90deg, brown o, green); // #3241
// ^error{Unknown unqualified identifier 'o'}
property<color> g16: @linear-gradient(0deg, red, green, blue); // #6819
// ^warning{Narrowing conversion from brush to color. This can lead to unexpected behavior because the brush is a gradient}
}

View file

@ -25,4 +25,7 @@ export X := Rectangle {
property<brush> g11: @radial-gradient(circle,);
property<brush> g12: @radial-gradient(circle);
property<color> g13: @radial-gradient(circle, red, green, blue); // #6819
// ^warning{Narrowing conversion from brush to color. This can lead to unexpected behavior because the brush is a gradient}
}