From 6a23949f07f4eba38086b656e7073ce3bf8cd2fe Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 13 Jun 2025 22:13:00 -0700 Subject: Allow interface methods to have default implementations. (#7439) --- source/slang/slang-lookup.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'source/slang/slang-lookup.cpp') diff --git a/source/slang/slang-lookup.cpp b/source/slang/slang-lookup.cpp index d03d763b8..604cee2b7 100644 --- a/source/slang/slang-lookup.cpp +++ b/source/slang/slang-lookup.cpp @@ -977,6 +977,35 @@ static void _lookUpInScopes( nullptr); } + if (auto defaultImplDecl = as(containerDecl)) + { + // If we are checking an interface default method implementation, + // we should look up members from implicit `this` whose type is the explicit `This` + // generic parameter, and skip looking up in the interface decl itself. + + // Instead of looking up in the interface decl itself, we should + // look up in the `This` type instead. + if (getText(name) != "This") + { + BreadcrumbInfo breadcrumb; + breadcrumb.kind = LookupResultItem::Breadcrumb::Kind::This; + breadcrumb.thisParameterMode = thisParameterMode; + breadcrumb.declRef = DeclRef(defaultImplDecl->thisTypeDecl); + breadcrumb.prev = nullptr; + Type* type = DeclRefType::create(astBuilder, breadcrumb.declRef); + _lookUpMembersInType(astBuilder, name, type, request, result, &breadcrumb); + } + + // We need to skip looking up in the interface decl itself, since we are + // looking up in the implicit `this` type. + for (; scope && !as(scope->containerDecl); scope = scope->parent) + { + // We need to skip looking up in the interface decl itself, since we are + // looking up in the implicit `this` type. + } + break; + } + // Before we proceed up to the next outer scope to perform lookup // again, we need to consider what the current scope tells us // about how to interpret uses of implicit `this` or `This`. For -- cgit v1.2.3