Skip to content

Commit

Permalink
Adjust some more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tatiana-s committed Jan 16, 2025
1 parent 43f7110 commit 7726524
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions guppylang/checker/errors/linearity.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Assign(Help):


@dataclass(frozen=True)
class LinearCaptureError(Error):
class NonCopyableCaptureError(Error):
title: ClassVar[str] = "Linearity violation"
span_label: ClassVar[str] = (
"{var.describe} with linear type {var.ty} cannot be used here since `{var}` is "
Expand All @@ -245,7 +245,7 @@ class DefinedHere(Note):


@dataclass(frozen=True)
class LinearPartialApplyError(Error):
class NonCopyablePartialApplyError(Error):
title: ClassVar[str] = "Linearity violation"
span_label: ClassVar[str] = (
"This expression implicitly constructs a closure that captures a linear value"
Expand All @@ -260,11 +260,11 @@ class Captured(Note):


@dataclass(frozen=True)
class LinearForBreakError(Error):
class NonDroppableForBreakError(Error):
title: ClassVar[str] = "Break in linear loop"
span_label: ClassVar[str] = "Early exit in linear loops is not allowed"

@dataclass(frozen=True)
class LinearIteratorType(Note):
class NonDroppableIteratorType(Note):
span_label: ClassVar[str] = "Iterator has linear type `{ty}`"
ty: Type
8 changes: 5 additions & 3 deletions guppylang/checker/expr_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
Variable,
)
from guppylang.checker.errors.generic import ExpectedError, UnsupportedError
from guppylang.checker.errors.linearity import LinearForBreakError
from guppylang.checker.errors.linearity import NonDroppableForBreakError
from guppylang.checker.errors.py_errors import (
IllegalPyExpressionError,
PyExprEvalError,
Expand Down Expand Up @@ -708,8 +708,10 @@ def visit_MakeIter(self, node: MakeIter) -> tuple[ast.expr, Type]:
node.origin_node
)
if breaks:
err = LinearForBreakError(breaks[0])
err.add_sub_diagnostic(LinearForBreakError.LinearIteratorType(node, ty))
err = NonDroppableForBreakError(breaks[0])
err.add_sub_diagnostic(
NonDroppableForBreakError.NonDroppableIteratorType(node, ty)
)
raise GuppyTypeError(err)
return expr, ty

Expand Down
16 changes: 9 additions & 7 deletions guppylang/checker/linearity_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
BorrowSubPlaceUsedError,
ComprAlreadyUsedError,
DropAfterCallError,
LinearCaptureError,
LinearPartialApplyError,
MoveOutOfSubscriptError,
NonCopyableCaptureError,
NonCopyablePartialApplyError,
NotOwnedError,
PlaceNotUsedError,
UnnamedExprNotUsedError,
Expand Down Expand Up @@ -376,9 +376,9 @@ def visit_PartialApply(self, node: PartialApply) -> None:
self.visit(node.func)
for arg in node.args:
ty = get_type(arg)
if ty.linear:
err = LinearPartialApplyError(node)
err.add_sub_diagnostic(LinearPartialApplyError.Captured(arg, ty))
if not ty.copyable:
err = NonCopyablePartialApplyError(node)
err.add_sub_diagnostic(NonCopyablePartialApplyError.Captured(arg, ty))
raise GuppyError(err)
self.visit(arg)

Expand Down Expand Up @@ -431,8 +431,10 @@ def visit_CheckedNestedFunctionDef(self, node: CheckedNestedFunctionDef) -> None
# TODO: In the future, we could support capturing of non-linear subplaces
for var, use in node.captured.values():
if not var.ty.copyable:
err = LinearCaptureError(use, var)
err.add_sub_diagnostic(LinearCaptureError.DefinedHere(var.defined_at))
err = NonCopyableCaptureError(use, var)
err.add_sub_diagnostic(
NonCopyableCaptureError.DefinedHere(var.defined_at)
)
raise GuppyError(err)
for place in leaf_places(var):
self.scope.use(place.id, use, UseKind.COPY)
Expand Down

0 comments on commit 7726524

Please sign in to comment.