Skip to content

[Name lookup] Don't look at a type's members when checking inherited types #18720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Aug 15, 2018

Teach unqualified name lookup not to look at the members of a nominal type or extension therefore when performing lookup in the inheritance clause of nominal type or extension. This was previously implemented in the type checker, but sink this logic down into UnqualifiedLookup, removing it from the type checker.

Built on top of @jrose-apple's excellent start.

jrose-apple and others added 5 commits August 14, 2018 23:17
There's already a field for this in GenericContext, which ProtocolDecl
indirectly inherits. Protocols may have slightly different treatment
of their where-clauses, but not enough to justify a separate field.

No functionality change.
Without this, we were firing off way more InheritedDeclsReferenced-
Requests than were actually needed. This drops it way down in the
profile, for what I /think/ is minor wins in type-checking the
stdlib...but it's hard to tell, since the time varies from run to run.
We only look for members within the extension’s type when we are in the
body or a protocol extension’s where clause.
The name-lookup behavior that avoids looking for members of a nominal type
or extension therefore when resolving the inheritance clause is now
handled by UnqualifiedLookup, so remove it from the type checker itself.

Fixes rdar://problem/39130543.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please test compiler performance

lib/AST/Type.cpp Outdated
@@ -3192,7 +3192,8 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,

t = t->getSuperclass(useArchetypes);
}
llvm_unreachable("no inheritance relationship between given classes");

return ErrorType::get(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did we end up here? Name lookup shouldn't be finding members of unrelated classes...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that Decl-based name lookup is finding something via a superclass (decl), but the types are invalid so we don't see them in the superclass (Type) chain. I can try to narrow this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 50763fc

} else if (auto func = dyn_cast<AbstractFunctionDecl>(DC)) {
DC = func;
options = TypeResolutionOptions(TypeResolverContext::GenericSignature);
} else if (!DC->isModuleScopeContext()) {
// Skip the generic parameter's context entirely.
DC = DC->getParent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the only other case is a SubscriptDecl, and it seems like this whole series of if/else if statements could just be removed? Just set DC = decl->getDeclContext() and be done with it.

Copy link
Member Author

@DougGregor DougGregor Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was planning to rewrite this in terms of InheritedTypeRequest, because this whole pile of code is redundant, but for now I'll simplify it down (here and in TypeCheckRequestFunctions.cpp).

@@ -1,5 +1,5 @@
// RUN: not --crash %target-swift-frontend %s -emit-ir
// REQUIRES: asserts
// RUN: %target-swift-frontend %s -emit-ir
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not-crashing for the wrong reasons still :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, right. This one. Thanks for the reminder


// When we have no source-location information, we have to perform member
// lookuo.
if (loc.isInvalid() || decl->getBraces().isInvalid())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lookuo -> lookup :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks

@swift-ci
Copy link
Contributor

Build comment file:

Summary for master full

Unexpected test results, excluded stats for RxSwift, ReactiveCocoa, Wordy, DatabaseKit, ReactiveSwift

Regressions found (see below)

Debug-batch

debug-batch brief

Regressed (0)
name old new delta delta_pct
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (3)
name old new delta delta_pct
Frontend.NumInstructions 10,547,712,277,291 10,485,147,195,290 -62,565,082,001 -0.59%
LLVM.NumLLVMBytesOutput 502,675,328 505,223,088 2,547,760 0.51%
time.swift-driver.wall 1176.3s 1177.3s 942.1ms 0.08%

debug-batch detailed

Regressed (4)
name old new delta delta_pct
AST.NumSourceLinesPerSecond 671,199 678,121 6,922 1.03% ⛔
IRModule.NumIRGlobals 1,151,134 1,163,441 12,307 1.07% ⛔
SILModule.NumSILGenFunctions 922,644 948,428 25,784 2.79% ⛔
Sema.SelfBoundsFromWhereClauseRequest 45,810 50,582 4,772 10.42% ⛔
Improved (9)
name old new delta delta_pct
AST.NumTotalClangImportedEntities 2,703,766 2,642,034 -61,732 -2.28% ✅
Driver.NumDriverPipePolls 86,576 83,008 -3,568 -4.12% ✅
Driver.NumDriverPipeReads 89,825 86,278 -3,547 -3.95% ✅
Sema.InheritedDeclsReferencedRequest 58,145,998 56,238,263 -1,907,735 -3.28% ✅
Sema.NamedLazyMemberLoadFailureCount 16,004 14,826 -1,178 -7.36% ✅
Sema.NamedLazyMemberLoadSuccessCount 2,604,260 2,202,306 -401,954 -15.43% ✅
Sema.NominalTypeLookupDirectCount 17,758,753 15,566,849 -2,191,904 -12.34% ✅
Sema.SuperclassDeclRequest 44,667,123 43,444,012 -1,223,111 -2.74% ✅
Sema.UnderlyingTypeDeclsReferencedRequest 2,606,514 1,628,313 -978,201 -37.53% ✅
Unchanged (delta < 1.0% or delta < 100.0ms) (76)
name old new delta delta_pct
AST.NumASTBytesAllocated 13,773,445,649 13,684,212,387 -89,233,262 -0.65%
AST.NumDecls 33,060 33,060 0 0.0%
AST.NumDependencies 88,574 88,574 0 0.0%
AST.NumImportedExternalDefinitions 824,474 816,537 -7,937 -0.96%
AST.NumInfixOperators 13,323 13,323 0 0.0%
AST.NumLinkLibraries 0 0 0 0.0%
AST.NumLoadedModules 100,529 100,529 0 0.0%
AST.NumLocalTypeDecls 13 13 0 0.0%
AST.NumObjCMethods 12,906 12,906 0 0.0%
AST.NumPostfixOperators 14 14 0 0.0%
AST.NumPrecedenceGroups 6,996 6,996 0 0.0%
AST.NumPrefixOperators 60 60 0 0.0%
AST.NumReferencedDynamicNames 45 45 0 0.0%
AST.NumReferencedMemberNames 1,912,092 1,899,880 -12,212 -0.64%
AST.NumReferencedTopLevelNames 115,785 115,797 12 0.01%
AST.NumSourceBuffers 151,367 151,367 0 0.0%
AST.NumSourceLines 1,198,590 1,198,590 0 0.0%
AST.NumUsedConformances 101,090 101,090 0 0.0%
Driver.ChildrenMaxRSS 34,703,523,840 34,676,183,040 -27,340,800 -0.08%
Driver.DriverDepCascadingDynamic 0 0 0 0.0%
Driver.DriverDepCascadingExternal 0 0 0 0.0%
Driver.DriverDepCascadingMember 0 0 0 0.0%
Driver.DriverDepCascadingNominal 0 0 0 0.0%
Driver.DriverDepCascadingTopLevel 0 0 0 0.0%
Driver.DriverDepDynamic 0 0 0 0.0%
Driver.DriverDepExternal 0 0 0 0.0%
Driver.DriverDepMember 0 0 0 0.0%
Driver.DriverDepNominal 0 0 0 0.0%
Driver.DriverDepTopLevel 0 0 0 0.0%
Driver.NumDriverJobsRun 7,072 7,072 0 0.0%
Driver.NumDriverJobsSkipped 0 0 0 0.0%
Driver.NumProcessFailures 0 0 0 0.0%
Frontend.NumInstructions 10,547,712,277,291 10,485,147,195,290 -62,565,082,001 -0.59%
Frontend.NumProcessFailures 0 0 0 0.0%
IRModule.NumIRAliases 26,417 26,417 0 0.0%
IRModule.NumIRBasicBlocks 1,726,493 1,730,602 4,109 0.24%
IRModule.NumIRComdatSymbols 0 0 0 0.0%
IRModule.NumIRFunctions 946,335 950,248 3,913 0.41%
IRModule.NumIRIFuncs 0 0 0 0.0%
IRModule.NumIRInsts 20,306,352 20,379,959 73,607 0.36%
IRModule.NumIRNamedMetaData 34,928 34,928 0 0.0%
IRModule.NumIRValueSymbols 1,817,659 1,829,770 12,111 0.67%
LLVM.NumLLVMBytesOutput 502,675,328 505,223,088 2,547,760 0.51%
Parse.NumFunctionsParsed 63,471 63,471 0 0.0%
SILModule.NumSILGenDefaultWitnessTables 0 0 0 0.0%
SILModule.NumSILGenGlobalVariables 14,900 14,900 0 0.0%
SILModule.NumSILGenVtables 3,966 3,966 0 0.0%
SILModule.NumSILGenWitnessTables 18,802 18,802 0 0.0%
SILModule.NumSILOptDefaultWitnessTables 0 0 0 0.0%
SILModule.NumSILOptFunctions 709,424 713,547 4,123 0.58%
SILModule.NumSILOptGlobalVariables 15,334 15,334 0 0.0%
SILModule.NumSILOptVtables 7,636 7,636 0 0.0%
SILModule.NumSILOptWitnessTables 36,779 36,779 0 0.0%
Sema.AccessLevelRequest 924,796 924,796 0 0.0%
Sema.DefaultAndMaxAccessLevelRequest 21,506 21,506 0 0.0%
Sema.EnumRawTypeRequest 7,761 7,688 -73 -0.94%
Sema.ExtendedNominalRequest 1,487,694 1,479,501 -8,193 -0.55%
Sema.InheritedTypeRequest 26,183 26,183 0 0.0%
Sema.IsDynamicRequest 725,423 725,819 396 0.05%
Sema.IsObjCRequest 649,266 649,667 401 0.06%
Sema.NumConformancesDeserialized 2,453,475 2,451,391 -2,084 -0.08%
Sema.NumConstraintScopes 7,336,968 7,336,400 -568 -0.01%
Sema.NumConstraintsConsideredForEdgeContraction 13,156,031 13,154,895 -1,136 -0.01%
Sema.NumDeclsDeserialized 16,649,336 16,547,503 -101,833 -0.61%
Sema.NumDeclsValidated 1,028,574 1,028,574 0 0.0%
Sema.NumFunctionsTypechecked 477,318 477,315 -3 -0.0%
Sema.NumGenericSignatureBuilders 789,064 787,165 -1,899 -0.24%
Sema.NumLazyGenericEnvironments 3,083,107 3,076,562 -6,545 -0.21%
Sema.NumLazyGenericEnvironmentsLoaded 310,593 308,975 -1,618 -0.52%
Sema.NumLazyIterableDeclContexts 2,810,704 2,801,549 -9,155 -0.33%
Sema.NumTypesDeserialized 7,398,843 7,373,705 -25,138 -0.34%
Sema.NumTypesValidated 870,165 870,165 0 0.0%
Sema.NumUnloadedLazyIterableDeclContexts 1,900,708 1,906,145 5,437 0.29%
Sema.OverriddenDeclsRequest 1,502,049 1,497,828 -4,221 -0.28%
Sema.SetterAccessLevelRequest 56,039 56,039 0 0.0%
Sema.SuperclassTypeRequest 15,741 15,741 0 0.0%

Release

release brief

Regressed (0)
name old new delta delta_pct
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (3)
name old new delta delta_pct
Frontend.NumInstructions 12,148,686,844,531 12,145,969,916,935 -2,716,927,596 -0.02%
LLVM.NumLLVMBytesOutput 478,284,120 478,284,174 54 0.0%
time.swift-driver.wall 2153.2s 2152.8s -390.0ms -0.02%

release detailed

Regressed (0)
name old new delta delta_pct
Improved (0)
name old new delta delta_pct
Unchanged (delta < 1.0% or delta < 100.0ms) (23)
name old new delta delta_pct
AST.NumImportedExternalDefinitions 146,166 146,062 -104 -0.07%
AST.NumLoadedModules 6,370 6,370 0 0.0%
AST.NumTotalClangImportedEntities 465,526 464,976 -550 -0.12%
AST.NumUsedConformances 103,289 103,289 0 0.0%
IRModule.NumIRBasicBlocks 1,703,286 1,703,286 0 0.0%
IRModule.NumIRFunctions 763,509 763,509 0 0.0%
IRModule.NumIRGlobals 883,635 883,635 0 0.0%
IRModule.NumIRInsts 14,676,244 14,676,244 0 0.0%
IRModule.NumIRValueSymbols 1,494,493 1,494,493 0 0.0%
LLVM.NumLLVMBytesOutput 478,284,120 478,284,174 54 0.0%
SILModule.NumSILGenFunctions 335,122 335,122 0 0.0%
SILModule.NumSILOptFunctions 452,939 452,939 0 0.0%
Sema.NumConformancesDeserialized 1,099,299 1,099,299 0 0.0%
Sema.NumConstraintScopes 7,239,682 7,239,654 -28 -0.0%
Sema.NumDeclsDeserialized 2,979,433 2,979,361 -72 -0.0%
Sema.NumDeclsValidated 548,486 548,486 0 0.0%
Sema.NumFunctionsTypechecked 172,341 172,313 -28 -0.02%
Sema.NumGenericSignatureBuilders 115,369 115,388 19 0.02%
Sema.NumLazyGenericEnvironments 501,319 501,320 1 0.0%
Sema.NumLazyGenericEnvironmentsLoaded 58,075 58,075 0 0.0%
Sema.NumLazyIterableDeclContexts 329,242 329,234 -8 -0.0%
Sema.NumTypesDeserialized 1,868,349 1,868,328 -21 -0.0%
Sema.NumTypesValidated 273,787 273,787 0 0.0%

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

Those debug-build improvements look nice. Yay.

…rotocols.

Add all of the generic parameters from outer contexts as well. This only
occurs in ill-formed code, but maintains a GenericSignatureBuilder
invariant that it knows about all of the generic parameters in play.
…erclassForDecl()

When we fail to find a particular superclass type of a type in ill-formed
code, make sure that we were right to look here at all. This re-establishes
a check I had recently weakened.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit be0e164 into swiftlang:master Aug 15, 2018
@DougGregor DougGregor deleted the kill-type-resolver-context-generic-signature branch August 15, 2018 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants