Skip to main content

apply_contextual_type

Function apply_contextual_type 

Source
pub fn apply_contextual_type(
    interner: &dyn TypeDatabase,
    expr_type: TypeId,
    contextual_type: Option<TypeId>,
) -> TypeId
Expand description

Apply contextual type to infer a more specific type.

This implements bidirectional type inference:

  1. If expr_type is any/unknown/error, use contextual type
  2. If expr_type is a literal and contextual type is a union containing that literal’s base type, preserve literal
  3. If expr_type is assignable to contextual type and is more specific, use expr_type
  4. Otherwise, prefer expr_type (don’t widen to contextual type)