Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

red-knot: infer string literal types #13113

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ pub enum Type<'db> {
IntLiteral(i64),
/// A boolean literal, either `True` or `False`.
BooleanLiteral(bool),
/// A string literal
StringLiteral(StringLiteralType<'db>),
/// A bytes literal
BytesLiteral(BytesLiteralType<'db>),
// TODO protocols, callable types, overloads, generics, type vars
Expand Down Expand Up @@ -278,6 +280,11 @@ impl<'db> Type<'db> {
Type::Unknown
}
Type::BooleanLiteral(_) => Type::Unknown,
Type::StringLiteral(_) => {
// TODO fix this comment with whatever it is we should say we
// want to do in the future.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this is probably something in the space of what we say for BytesLiteral?

Type::Unknown
}
Type::BytesLiteral(_) => {
// TODO defer to Type::Instance(<bytes from typeshed>).member
Type::Unknown
Expand Down Expand Up @@ -378,6 +385,12 @@ pub struct IntersectionType<'db> {
negative: FxOrderSet<Type<'db>>,
}

#[salsa::interned]
pub struct StringLiteralType<'db> {
#[return_ref]
value: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use Box<str> here, to save a bit of space, since the data should always be immutable? Though it maybe wouldn't make much difference

Copy link
Contributor

@carljm carljm Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call; this is probably worth doing, considering it saves an entire usize of memory, which is eight ascii characters (on a 64-bit system); it could halve the memory used for a lot of small string literal types. (Not really, if you consider the Salsa interning overhead as well. But still, not insignificant if there are lots of small string literals.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Yeah, I consistently forget that Box<str> is an option. 👍🏼

}

#[salsa::interned]
pub struct BytesLiteralType<'db> {
#[return_ref]
Expand Down
12 changes: 11 additions & 1 deletion crates/red_knot_python_semantic/src/types/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fmt::{Display, Formatter};

use ruff_python_ast::str::Quote;
use ruff_python_literal::escape::AsciiEscape;
use ruff_python_literal::escape::{AsciiEscape, Escape};

use crate::types::{IntersectionType, Type, UnionType};
use crate::Db;
Expand Down Expand Up @@ -49,6 +49,16 @@ impl Display for DisplayType<'_> {
escape.bytes_repr().write(f)?;
f.write_str("]")
}
Type::StringLiteral(string) => {
let escape = AsciiEscape::with_preferred_quote(
string.value(self.db).as_bytes(),
Quote::Double,
);
carljm marked this conversation as resolved.
Show resolved Hide resolved

f.write_str("Literal[")?;
escape.write_body(f)?;
f.write_str("]")
}
}
}
}
Expand Down
40 changes: 36 additions & 4 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::semantic_index::SemanticIndex;
use crate::types::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics};
use crate::types::{
builtins_symbol_ty_by_name, definitions_ty, global_symbol_ty_by_name, BytesLiteralType,
ClassType, FunctionType, Name, Type, UnionBuilder,
ClassType, FunctionType, Name, StringLiteralType, Type, UnionBuilder,
};
use crate::Db;

Expand Down Expand Up @@ -1243,9 +1243,8 @@ impl<'db> TypeInferenceBuilder<'db> {
}

#[allow(clippy::unused_self)]
fn infer_string_literal_expression(&mut self, _literal: &ast::ExprStringLiteral) -> Type<'db> {
// TODO Literal["..."] or str
Type::Unknown
fn infer_string_literal_expression(&mut self, literal: &ast::ExprStringLiteral) -> Type<'db> {
Type::StringLiteral(StringLiteralType::new(self.db, literal.value.to_string()))
}

#[allow(clippy::unused_self)]
Expand Down Expand Up @@ -1785,6 +1784,17 @@ impl<'db> TypeInferenceBuilder<'db> {
_ => Type::Unknown, // TODO
}
}
Type::StringLiteral(lhs) => match right_ty {
Type::StringLiteral(rhs) => match op {
ast::Operator::Add => Type::StringLiteral(StringLiteralType::new(self.db, {
let lhs_value = lhs.value(self.db);
let rhs_value = rhs.value(self.db);
lhs_value.clone() + rhs_value
})),
_ => Type::Unknown, // TODO
},
_ => Type::Unknown, // TODO
},
_ => Type::Unknown, // TODO
}
}
Expand Down Expand Up @@ -2298,6 +2308,28 @@ mod tests {
Ok(())
}

#[test]
fn string_type() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"src/a.py",
r#"
w = "Hello"
x = 'world'
y = "Guten " + 'tag'
z = 'bon ' + "jour"
"#,
)?;

assert_public_ty(&db, "src/a.py", "w", "Literal[Hello]");
assert_public_ty(&db, "src/a.py", "x", "Literal[world]");
assert_public_ty(&db, "src/a.py", "y", "Literal[Guten tag]");
assert_public_ty(&db, "src/a.py", "z", "Literal[bon jour]");
carljm marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

#[test]
fn bytes_type() -> anyhow::Result<()> {
let mut db = setup_db();
Expand Down
Loading