Skip to content

Commit

Permalink
Fix bug: IgnoreInheritance in lookup (#6146)
Browse files Browse the repository at this point in the history
* Fix bug: IgnoreInheritance in lookup

When specifying IgnoreInheritance in lookup, it will ignore
all members in the self extension for generic, the reason is
that it doesn't specialize the target type of the extension
decl when comparing with self type, so it will result that every
type is unequal to the target type.
  • Loading branch information
kaizhangNV authored Jan 21, 2025
1 parent 4c2c085 commit 151bdb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 3 additions & 2 deletions source/slang/slang-check-overload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2167,15 +2167,16 @@ void SemanticsVisitor::AddTypeOverloadCandidates(Type* type, OverloadResolveCont
// from a value of the same type. There is no need in Slang for
// "copy constructors" but the core module currently has to define
// some just to make code that does, e.g., `float(1.0f)` work.)

LookupOptions options =
LookupOptions(uint8_t(LookupOptions::IgnoreInheritance) | uint8_t(LookupOptions::NoDeref));
LookupResult initializers = lookUpMember(
m_astBuilder,
this,
getName("$init"),
type,
context.sourceScope,
LookupMask::Default,
LookupOptions::NoDeref);
options);

AddOverloadCandidates(initializers, context);
}
Expand Down
20 changes: 16 additions & 4 deletions source/slang/slang-lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ static void _lookupMembersInSuperTypeFacets(
continue;
}

auto extensionFacet = as<ExtensionDecl>(facet.getImpl()->getDeclRef().getDecl());

// If we are looking up in an interface, and the lookup request told us
// to skip interfaces, we should do so here.
if (auto baseInterfaceDeclRef = containerDeclRef.as<InterfaceDecl>())
Expand All @@ -448,10 +448,22 @@ static void _lookupMembersInSuperTypeFacets(
// "Self"
else if (
int(request.options) & int(LookupOptions::IgnoreInheritance) &&
(facet.getImpl()->directness != Facet::Directness::Self &&
(!extensionFacet || !extensionFacet->targetType.type->equals(selfType))))
(facet.getImpl()->directness != Facet::Directness::Self))
{
continue;
if (auto extensionDeclRef = facet.getImpl()->getDeclRef().as<ExtensionDecl>())
{
if (auto targetType = getTargetType(astBuilder, extensionDeclRef))
{
if (!targetType->equals(selfType))
{
// If the extension is to the same type as the one we are looking up in, we
// should include it in the lookup.
continue;
}
}
}
else
continue;
}

// Some things that are syntactically `InheritanceDecl`s don't actually
Expand Down

0 comments on commit 151bdb8

Please sign in to comment.