This is an archive of the discontinued LLVM Phabricator instance.

[C++20] [Modules] Exit early if export decl is invalid
ClosedPublic

Authored by ChuanqiXu on Jan 12 2022, 1:27 AM.

Details

Summary

See https://godbolt.org/z/Ka9foTov4, the following simple example would make the compiler crash:

export struct Unit {
    bool operator<(const Unit&);
};

The problem here is that we would set module ownership for the export decl even if we know it is invalid. And for decls who owns a module ownership, the Sema would assume there is a module for it. So here is the crash.

Another straight forward fix would be https://reviews.llvm.org/D117094. But I prefer the current revision. Since it didn't add new branch and it is more easy to read. I guess people reading D117094 without a background might be confusing. "Why is here a branch?". But this patch is more simple. We meet an error so we exit directly. So I prefer this one.

Diff Detail

Event Timeline

ChuanqiXu edited the summary of this revision. (Show Details)Jan 12 2022, 1:32 AM
ChuanqiXu set the repository for this revision to rG LLVM Github Monorepo.
ChuanqiXu added a project: Restricted Project.
aaron.ballman added inline comments.Jan 12 2022, 1:01 PM
clang/lib/Sema/SemaModule.cpp
530–531

Am I understanding properly that this moved up here so that the for loop on line 553 can traverse the new context?

If so, can it be moved down to immediately before the for loop?

539–550

It seems a bit suspicious to me that we call D->getInvalidDecl() below within the for loop, but all the other places we leave it as a valid declaration despite it causing error diagnostics.

ChuanqiXu updated this revision to Diff 399526.Jan 12 2022, 6:44 PM

Address comments.

ChuanqiXu added inline comments.Jan 12 2022, 6:47 PM
clang/lib/Sema/SemaModule.cpp
530–531

The intention to move these up is to make sure D could be put in the context even error detected. It wouldn't affect the traverse on line 556 since an ExportDecl wouldn't be a NamespaceDecl.

539–550

Yeah, it is intentional to call D->setInvalidDecl() this loop. It would suppress more diagnostic messages. But I feel it is good to call D->setInvalidDecl() in other places.

aaron.ballman accepted this revision.Jan 13 2022, 11:30 AM

LGTM!

clang/lib/Sema/SemaModule.cpp
530–531

Ah yes, that makes far more sense. Thanks!

539–550

Thanks, I agree that we want to mark the decl invalid rather than leave it seeming valid.

This revision is now accepted and ready to land.Jan 13 2022, 11:30 AM