summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-06-12 07:57:42 -0700
committerGitHub <noreply@github.com>2025-06-12 07:57:42 -0700
commit4ae6e9d8b7790d827ca9edd729ad94f38a0c73de (patch)
tree1aad23cae09dacf9d3377d040cbc9ac84b48675e /source
parent7dad68f869502e5c0ab32c12cbf8db866e020713 (diff)
Diagnose on use of struct inheritance. (#7419)
* Diagnose on use of struct inheritance. * fix test. * Fix tests. * fix. --------- Co-authored-by: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com>
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-check-decl.cpp24
-rw-r--r--source/slang/slang-diagnostic-defs.h11
2 files changed, 30 insertions, 5 deletions
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp
index 2865188b4..ddc4ec4d5 100644
--- a/source/slang/slang-check-decl.cpp
+++ b/source/slang/slang-check-decl.cpp
@@ -7811,7 +7811,7 @@ void SemanticsDeclBasesVisitor::visitStructDecl(StructDecl* decl)
{
getSink()->diagnose(
inheritanceDecl,
- Diagnostics::baseOfStructMustBeStructOrInterface,
+ Diagnostics::baseOfStructMustBeInterface,
decl,
baseType);
continue;
@@ -7823,6 +7823,26 @@ void SemanticsDeclBasesVisitor::visitStructDecl(StructDecl* decl)
}
else if (auto baseStructDeclRef = baseDeclRef.as<StructDecl>())
{
+ if (!isFromCoreModule(decl))
+ {
+ // In Slang 2026, we no longer allow structs to inherit from other structs.
+ if (isSlang2026OrLater(this))
+ {
+ getSink()->diagnose(
+ inheritanceDecl,
+ Diagnostics::baseOfStructMustBeInterface,
+ decl,
+ baseType);
+ }
+ else
+ {
+ // For legacy langauge versions, we still allow struct inheritance to avoid
+ // breaking existing code, but we will emit a warning to inform the user
+ // that this feature is unstable and may be removed in the future.
+ getSink()->diagnose(inheritanceDecl, Diagnostics::inheritanceUnstable);
+ }
+ }
+
// To simplify the task of reading and maintaining code,
// we require that when a `struct` inherits from another
// `struct`, the base `struct` is the first item in
@@ -7845,7 +7865,7 @@ void SemanticsDeclBasesVisitor::visitStructDecl(StructDecl* decl)
{
getSink()->diagnose(
inheritanceDecl,
- Diagnostics::baseOfStructMustBeStructOrInterface,
+ Diagnostics::baseOfStructMustBeInterface,
decl,
baseType);
continue;
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index f4741ab04..4f0edf53b 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -1662,8 +1662,8 @@ DIAGNOSTIC(
DIAGNOSTIC(
30811,
Error,
- baseOfStructMustBeStructOrInterface,
- "struct '$0' cannot inherit from type '$1' that is neither a struct nor an interface")
+ baseOfStructMustBeInterface,
+ "struct '$0' cannot inherit from non-interface type '$1'")
DIAGNOSTIC(
30812,
Error,
@@ -1681,7 +1681,12 @@ DIAGNOSTIC(
baseOfClassMustBeClassOrInterface,
"class '$0' cannot inherit from type '$1' that is neither a class nor an interface")
DIAGNOSTIC(30815, Error, circularityInExtension, "circular extension is not allowed.")
-
+DIAGNOSTIC(
+ 30816,
+ Warning,
+ inheritanceUnstable,
+ "support for inheritance is unstable and will be removed in future language versions, consider "
+ "using composition instead.")
DIAGNOSTIC(
30820,
Error,