-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Make IRDL operations and attributes final (#2159)
It was previously possible to inherit an operation or attribute. While this may be useful, it is also pretty dangerous, as now a constraint such as `operand_def(MyAttribute)` will also accept other attributes such as `MyInheritedAttribute`, which likely shouldn't be accepted. For instance, it was previously possible to use `riscv.fastmathflags` in the LLVM dialect, as the attribute was inheriting `llvm.fastmathflags`. Moreover, it is necessary for the declarative format to know that a specific attribute type is expected, and this isn't possible if the attribute might be inherited. The solution I'm proposing here is to use a new `final` decorator inside `irdl_attr_definition` and `irdl_op_definition` to avoid these inheritances. Note that this `final` decorator will trigger an error if anyone tries to subclass the final class. Resolves: #2064
- Loading branch information
Showing
7 changed files
with
133 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
|
||
from xdsl.utils.runtime_final import is_runtime_final, runtime_final | ||
|
||
|
||
class NotFinal: | ||
"""A non-Final class.""" | ||
|
||
|
||
@runtime_final | ||
class Final: | ||
"""A Final class.""" | ||
|
||
|
||
def test_is_runtime_final(): | ||
"""Check that `is_runtime_final` returns the correct value.""" | ||
assert not is_runtime_final(NotFinal) | ||
assert is_runtime_final(Final) | ||
|
||
|
||
def test_final_inheritance_error(): | ||
"""Check that final classes cannot be subclassed.""" | ||
with pytest.raises(TypeError, match="Subclassing final classes is restricted"): | ||
|
||
class SubFinal(Final): | ||
pass | ||
|
||
SubFinal() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any, TypeVar | ||
|
||
|
||
def _init_subclass(cls: type, *args: Any, **kwargs: Any) -> None: | ||
"""Is used by `final` to prevent a class from being subclassed at runtime.""" | ||
raise TypeError("Subclassing final classes is restricted") | ||
|
||
|
||
C = TypeVar("C", bound=type) | ||
|
||
|
||
def runtime_final(cls: C) -> C: | ||
"""Prevent a class from being subclassed at runtime.""" | ||
|
||
# It is safe to discard the previous __init_subclass__ method as anyway | ||
# the new one will raise an error. | ||
setattr(cls, "__init_subclass__", classmethod(_init_subclass)) | ||
|
||
# This is a marker to check if a class is final or not. | ||
setattr(cls, "__final__", True) | ||
return cls | ||
|
||
|
||
def is_runtime_final(cls: type) -> bool: | ||
"""Check if a class is final.""" | ||
return hasattr(cls, "__final__") |