Move ExprConstant::kind to StringConstant::unicode (#7180)

This commit is contained in:
Dhruv Manilawala 2023-09-06 13:09:25 +05:30 committed by GitHub
parent 31990b8d3f
commit 04f2842e4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
119 changed files with 130 additions and 504 deletions

View file

@ -334,7 +334,7 @@ impl<'a> From<&'a ast::Decorator> for ComparableDecorator<'a> {
pub enum ComparableConstant<'a> {
None,
Bool(&'a bool),
Str(&'a str),
Str { value: &'a str, unicode: bool },
Bytes(&'a [u8]),
Int(&'a BigInt),
Tuple(Vec<ComparableConstant<'a>>),
@ -353,7 +353,11 @@ impl<'a> From<&'a ast::Constant> for ComparableConstant<'a> {
// Compare strings based on resolved value, not representation (i.e., ignore whether
// the string was implicitly concatenated).
implicit_concatenated: _,
}) => Self::Str(value),
unicode,
}) => Self::Str {
value,
unicode: *unicode,
},
ast::Constant::Bytes(ast::BytesConstant {
value,
// Compare bytes based on resolved value, not representation (i.e., ignore whether
@ -655,7 +659,6 @@ pub struct ExprFString<'a> {
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprConstant<'a> {
value: ComparableConstant<'a>,
kind: Option<&'a str>,
}
#[derive(Debug, PartialEq, Eq, Hash)]
@ -904,14 +907,11 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
}) => Self::FString(ExprFString {
values: values.iter().map(Into::into).collect(),
}),
ast::Expr::Constant(ast::ExprConstant {
value,
kind,
range: _,
}) => Self::Constant(ExprConstant {
value: value.into(),
kind: kind.as_ref().map(String::as_str),
}),
ast::Expr::Constant(ast::ExprConstant { value, range: _ }) => {
Self::Constant(ExprConstant {
value: value.into(),
})
}
ast::Expr::Attribute(ast::ExprAttribute {
value,
attr,

View file

@ -576,7 +576,6 @@ pub const fn is_const_none(expr: &Expr) -> bool {
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::None,
kind: None,
..
}),
)
@ -588,7 +587,6 @@ pub const fn is_const_true(expr: &Expr) -> bool {
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::Bool(true),
kind: None,
..
}),
)
@ -600,7 +598,6 @@ pub const fn is_const_false(expr: &Expr) -> bool {
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::Bool(false),
kind: None,
..
}),
)
@ -1190,17 +1187,14 @@ mod tests {
});
let constant_one = Expr::Constant(ExprConstant {
value: Constant::Int(1.into()),
kind: Some("x".to_string()),
range: TextRange::default(),
});
let constant_two = Expr::Constant(ExprConstant {
value: Constant::Int(2.into()),
kind: Some("y".to_string()),
range: TextRange::default(),
});
let constant_three = Expr::Constant(ExprConstant {
value: Constant::Int(3.into()),
kind: Some("z".to_string()),
range: TextRange::default(),
});
let type_var_one = TypeParam::TypeVar(TypeParamTypeVar {
@ -1243,7 +1237,6 @@ mod tests {
let bound = Expr::Constant(ExprConstant {
value: Constant::Int(1.into()),
kind: Some("x".to_string()),
range: TextRange::default(),
});

View file

@ -2682,11 +2682,7 @@ impl AstNode for ast::ExprConstant {
where
V: PreorderVisitor<'a> + ?Sized,
{
let ast::ExprConstant {
value,
range: _,
kind: _,
} = self;
let ast::ExprConstant { value, range: _ } = self;
visitor.visit_constant(value);
}
}

View file

@ -945,7 +945,6 @@ impl From<ExprFString> for Expr {
pub struct ExprConstant {
pub range: TextRange,
pub value: Constant,
pub kind: Option<String>,
}
impl From<ExprConstant> for Expr {
@ -2559,6 +2558,8 @@ pub struct StringConstant {
/// The string value as resolved by the parser (i.e., without quotes, or escape sequences, or
/// implicit concatenations).
pub value: String,
/// Whether the string is a Unicode string (i.e., `u"..."`).
pub unicode: bool,
/// Whether the string contains multiple string tokens that were implicitly concatenated.
pub implicit_concatenated: bool,
}
@ -2598,6 +2599,7 @@ impl From<String> for Constant {
fn from(value: String) -> Constant {
Self::Str(StringConstant {
value,
unicode: false,
implicit_concatenated: false,
})
}