From 4ae6e9d8b7790d827ca9edd729ad94f38a0c73de Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 12 Jun 2025 07:57:42 -0700 Subject: 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> --- source/slang/slang-check-decl.cpp | 24 ++++++++++++++++++++++-- source/slang/slang-diagnostic-defs.h | 11 ++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'source') 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()) { + 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, -- cgit v1.2.3