Skip to content

Commit

Permalink
Add add_ prefix to diagnostics builder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Oct 30, 2024
1 parent 9ed1562 commit 108be51
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ impl<'db> IterationOutcome<'db> {
match self {
Self::Iterable { element_ty } => element_ty,
Self::NotIterable { not_iterable_ty } => {
diagnostics.not_iterable(iterable_node, not_iterable_ty);
diagnostics.add_not_iterable(iterable_node, not_iterable_ty);
Type::Unknown
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/red_knot_python_semantic/src/types/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'db> TypeCheckDiagnosticsBuilder<'db> {
}

/// Emit a diagnostic declaring that the object represented by `node` is not iterable
pub(super) fn not_iterable(&mut self, node: AnyNodeRef, not_iterable_ty: Type<'db>) {
pub(super) fn add_not_iterable(&mut self, node: AnyNodeRef, not_iterable_ty: Type<'db>) {
self.add(
node,
"not-iterable",
Expand All @@ -142,7 +142,7 @@ impl<'db> TypeCheckDiagnosticsBuilder<'db> {
}

/// Emit a diagnostic declaring that an index is out of bounds for a tuple.
pub(super) fn index_out_of_bounds(
pub(super) fn add_index_out_of_bounds(
&mut self,
kind: &'static str,
node: AnyNodeRef,
Expand All @@ -161,7 +161,7 @@ impl<'db> TypeCheckDiagnosticsBuilder<'db> {
}

/// Emit a diagnostic declaring that a type does not support subscripting.
pub(super) fn non_subscriptable(
pub(super) fn add_non_subscriptable(
&mut self,
node: AnyNodeRef,
non_subscriptable_ty: Type<'db>,
Expand All @@ -177,7 +177,7 @@ impl<'db> TypeCheckDiagnosticsBuilder<'db> {
);
}

pub(super) fn unresolved_module(
pub(super) fn add_unresolved_module(
&mut self,
import_node: impl Into<AnyNodeRef<'db>>,
level: u32,
Expand All @@ -194,15 +194,15 @@ impl<'db> TypeCheckDiagnosticsBuilder<'db> {
);
}

pub(super) fn slice_step_size_zero(&mut self, node: AnyNodeRef) {
pub(super) fn add_slice_step_size_zero(&mut self, node: AnyNodeRef) {
self.add(
node,
"zero-stepsize-in-slice",
format_args!("Slice step size can not be zero"),
);
}

pub(super) fn invalid_assignment(
pub(super) fn add_invalid_assignment(
&mut self,
node: AnyNodeRef,
declared_ty: Type<'db>,
Expand Down
33 changes: 18 additions & 15 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ impl<'db> TypeInferenceBuilder<'db> {
);
if !bound_ty.is_assignable_to(self.db, declared_ty) {
self.diagnostics
.invalid_assignment(node, declared_ty, bound_ty);
.add_invalid_assignment(node, declared_ty, bound_ty);
// allow declarations to override inference in case of invalid assignment
bound_ty = declared_ty;
};
Expand Down Expand Up @@ -611,7 +611,7 @@ impl<'db> TypeInferenceBuilder<'db> {
inferred_ty
} else {
self.diagnostics
.invalid_assignment(node, declared_ty, inferred_ty);
.add_invalid_assignment(node, declared_ty, inferred_ty);
// if the assignment is invalid, fall back to assuming the annotation is correct
declared_ty
};
Expand Down Expand Up @@ -1542,7 +1542,7 @@ impl<'db> TypeInferenceBuilder<'db> {
if let Some(module) = self.module_ty_from_name(&module_name) {
module
} else {
self.diagnostics.unresolved_module(alias, 0, Some(name));
self.diagnostics.add_unresolved_module(alias, 0, Some(name));
Type::Unknown
}
} else {
Expand Down Expand Up @@ -1684,7 +1684,7 @@ impl<'db> TypeInferenceBuilder<'db> {
}
} else {
self.diagnostics
.unresolved_module(import_from, *level, module);
.add_unresolved_module(import_from, *level, module);
Type::Unknown
}
}
Expand All @@ -1699,7 +1699,7 @@ impl<'db> TypeInferenceBuilder<'db> {
format_import_from_module(*level, module),
);
self.diagnostics
.unresolved_module(import_from, *level, module);
.add_unresolved_module(import_from, *level, module);
Type::Unknown
}
Err(ModuleNameResolutionError::UnknownCurrentModule) => {
Expand All @@ -1709,7 +1709,7 @@ impl<'db> TypeInferenceBuilder<'db> {
self.file.path(self.db)
);
self.diagnostics
.unresolved_module(import_from, *level, module);
.add_unresolved_module(import_from, *level, module);
Type::Unknown
}
};
Expand Down Expand Up @@ -3205,7 +3205,7 @@ impl<'db> TypeInferenceBuilder<'db> {
.py_index(i32::try_from(int).expect("checked in branch arm"))
.copied()
.unwrap_or_else(|_| {
self.diagnostics.index_out_of_bounds(
self.diagnostics.add_index_out_of_bounds(
"tuple",
value_node.into(),
value_ty,
Expand All @@ -3224,7 +3224,7 @@ impl<'db> TypeInferenceBuilder<'db> {
let new_elements: Vec<_> = new_elements.copied().collect();
Type::Tuple(TupleType::new(self.db, new_elements.into_boxed_slice()))
} else {
self.diagnostics.slice_step_size_zero(value_node.into());
self.diagnostics.add_slice_step_size_zero(value_node.into());
Type::Unknown
}
}
Expand All @@ -3243,7 +3243,7 @@ impl<'db> TypeInferenceBuilder<'db> {
))
})
.unwrap_or_else(|_| {
self.diagnostics.index_out_of_bounds(
self.diagnostics.add_index_out_of_bounds(
"string",
value_node.into(),
value_ty,
Expand All @@ -3263,7 +3263,7 @@ impl<'db> TypeInferenceBuilder<'db> {
let literal: String = new_chars.collect();
Type::StringLiteral(StringLiteralType::new(self.db, literal.into_boxed_str()))
} else {
self.diagnostics.slice_step_size_zero(value_node.into());
self.diagnostics.add_slice_step_size_zero(value_node.into());
Type::Unknown
};
result
Expand All @@ -3280,7 +3280,7 @@ impl<'db> TypeInferenceBuilder<'db> {
Type::BytesLiteral(BytesLiteralType::new(self.db, [*byte].as_slice()))
})
.unwrap_or_else(|_| {
self.diagnostics.index_out_of_bounds(
self.diagnostics.add_index_out_of_bounds(
"bytes literal",
value_node.into(),
value_ty,
Expand All @@ -3299,7 +3299,7 @@ impl<'db> TypeInferenceBuilder<'db> {
let new_bytes: Vec<u8> = new_bytes.copied().collect();
Type::BytesLiteral(BytesLiteralType::new(self.db, new_bytes.into_boxed_slice()))
} else {
self.diagnostics.slice_step_size_zero(value_node.into());
self.diagnostics.add_slice_step_size_zero(value_node.into());
Type::Unknown
}
}
Expand Down Expand Up @@ -3372,14 +3372,17 @@ impl<'db> TypeInferenceBuilder<'db> {
return KnownClass::GenericAlias.to_instance(self.db);
}

self.diagnostics.non_subscriptable(
self.diagnostics.add_non_subscriptable(
value_node.into(),
value_ty,
"__class_getitem__",
);
} else {
self.diagnostics
.non_subscriptable(value_node.into(), value_ty, "__getitem__");
self.diagnostics.add_non_subscriptable(
value_node.into(),
value_ty,
"__getitem__",
);
}

Type::Unknown
Expand Down

0 comments on commit 108be51

Please sign in to comment.