Skip to content

[red-knot] Make Type::in_type_expression() exhaustive #15048

@AlexWaygood

Description

@AlexWaygood

Type::in_type_expression() already errors for some types that are invalid in type expression contexts:

// TODO: Should emit a diagnostic
Type::KnownInstance(KnownInstanceType::Annotated) => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::BareAnnotated],
fallback_type: Type::Unknown,
}),
Type::KnownInstance(KnownInstanceType::Literal) => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::BareLiteral],
fallback_type: Type::Unknown,
}),
Type::Todo(_) => Ok(*self),
_ => Ok(todo_type!(
"Unsupported or invalid type in a type expression"
)),

However, there are many other types that are invalid in type expression contexts. We should remove the final fallback branch from this match statement and make it exhaustive so that all typing errors are correctly reported.

I expect that many invalid types can be covered by a single branch. Doing so might require changing this method so that its return type is std::fmt::Arguments rather than &'static str:

impl InvalidTypeExpression {
const fn reason(self) -> &'static str {
match self {
Self::BareAnnotated => "`Annotated` requires at least two arguments when used in an annotation or type expression",
Self::BareLiteral => "`Literal` requires at least one argument when used in a type expression",
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions especially welcometyMulti-file analysis & type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions