diff --git a/parser/src/ast.rs b/parser/src/ast.rs index e3fb6afe..13d853cc 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -353,7 +353,7 @@ pub enum ConstantValue { None, Ellipsis, Bool, - Str, + Str(String), Bytes, Tuple, // Numbers are string because we don't care about the value rn. diff --git a/parser/src/parser/mod.rs b/parser/src/parser/mod.rs index 7789fdbe..e5dce4af 100644 --- a/parser/src/parser/mod.rs +++ b/parser/src/parser/mod.rs @@ -64,10 +64,10 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result { + (ConstantValue::Str(s), ConstantValue::Str(s2)) => { Expression::Constant(Box::new(Constant { node, - value: ConstantValue::Str, + value: ConstantValue::Str(s + s2.as_str()), })) } (ConstantValue::Bytes, ConstantValue::Bytes) => { @@ -100,10 +100,10 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result { let mut values = fstring_lhs.values; match const_rhs.value { - ConstantValue::Str => { + ConstantValue::Str(s) => { values.push(Expression::Constant(Box::new(Constant { node: const_rhs.node, - value: ConstantValue::Str, + value: ConstantValue::Str(s), }))); } ConstantValue::Bytes => { @@ -121,9 +121,9 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result { let const_expr = match const_lhs.value { - ConstantValue::Str => Expression::Constant(Box::new(Constant { + ConstantValue::Str(s) => Expression::Constant(Box::new(Constant { node: const_lhs.node, - value: ConstantValue::Str, + value: ConstantValue::Str(s), })), ConstantValue::Bytes => { panic!("Cannot concat string and bytes"); diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index 75c1cd2f..2118d8b7 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -2495,7 +2495,7 @@ impl<'a> Parser<'a> { self.bump_any(); Expression::Constant(Box::new(Constant { node: self.finish_node(start), - value: ConstantValue::Str, + value: ConstantValue::Str(string_val), })) } @@ -2657,7 +2657,7 @@ impl<'a> Parser<'a> { self.bump_any(); Expression::Constant(Box::new(Constant { node: self.finish_node(start), - value: ConstantValue::Str, + value: ConstantValue::Str(string_val), })) } @@ -3387,7 +3387,7 @@ impl<'a> Parser<'a> { self.bump(Kind::FStringMiddle); Ok(Expression::Constant(Box::new(Constant { node: self.finish_node(node), - value: ConstantValue::Str, + value: ConstantValue::Str(str_val), }))) } Kind::LeftBracket => self.parse_fstring_replacement_field(), diff --git a/typechecker/src/type_evaluator.rs b/typechecker/src/type_evaluator.rs index 93eec98f..92eefd97 100755 --- a/typechecker/src/type_evaluator.rs +++ b/typechecker/src/type_evaluator.rs @@ -107,7 +107,7 @@ impl<'a> TypeEvaluator<'a> { // typing.readthedocs.io/en/latest/spec/literal.html#backwards-compatibility ast::ConstantValue::Int => self.get_builtin_type("int"), ast::ConstantValue::Float => self.get_builtin_type("float"), - ast::ConstantValue::Str => self.get_builtin_type("str"), + ast::ConstantValue::Str(_) => self.get_builtin_type("str"), ast::ConstantValue::Bool => self.get_builtin_type("bool"), ast::ConstantValue::None => Some(PythonType::None), ast::ConstantValue::Bytes => self.get_builtin_type("bytes"), @@ -148,7 +148,7 @@ impl<'a> TypeEvaluator<'a> { }; let type_name = match first_arg { ast::Expression::Constant(str) => match &str.value { - ast::ConstantValue::Str => str.get_value(&self.file.source), + ast::ConstantValue::Str(s) => str.get_value(s), _ => panic!("TypeVar first arg must be a string"), }, _ => panic!("TypeVar must be called with at least one arg"), @@ -564,8 +564,8 @@ impl<'a> TypeEvaluator<'a> { // 2. Module is preferred over local scope so we first check module scope and // then local scope. // https://peps.python.org/pep-0563/#backwards-compatibility - ast::ConstantValue::Str => { - let mut parser = Parser::new(&self.file.source); + ast::ConstantValue::Str(ref s) => { + let mut parser = Parser::new(s); // Wrap the parsing logic inside a `catch_unwind` block let parse_result = catch_unwind(AssertUnwindSafe(|| parser.parse())); @@ -1326,7 +1326,7 @@ impl<'a> TypeEvaluator<'a> { ast::ConstantValue::Bool => LiteralValue::Bool, ast::ConstantValue::Int => LiteralValue::Int, ast::ConstantValue::Float => LiteralValue::Float, - ast::ConstantValue::Str => LiteralValue::Str, + ast::ConstantValue::Str(_) => LiteralValue::Str, ast::ConstantValue::Bytes => LiteralValue::Bytes, ast::ConstantValue::None => LiteralValue::None, // Tuple is illegal if it has parentheses, otherwise it's allowed and the output