This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Reject programs declaring namespace std to be inline
ClosedPublic

Authored by rZhBoYao on Jul 23 2023, 10:57 AM.

Details

Summary

Fixes #64041

Diff Detail

Event Timeline

rZhBoYao created this revision.Jul 23 2023, 10:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 23 2023, 10:57 AM
rZhBoYao requested review of this revision.Jul 23 2023, 10:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 23 2023, 10:57 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

https://eel.is/c++draft/namespace.std#7 is in the library clause.
Couldn't find a better place to put the test other than clang/test/CXX/dcl.dcl/basic.namespace/namespace.def/p7.cpp which is for https://eel.is/c++draft/namespace.def.general#4.sentence-2.

rZhBoYao edited the summary of this revision. (Show Details)Jul 23 2023, 11:33 AM

Can you add a test for foo::std ? I suspects it warns, which is incorrect ? Can you add additional tests for inline std::foo ? (which should warn)
Thanks!

MitalAshok requested changes to this revision.Jul 24 2023, 1:50 AM
MitalAshok added a subscriber: MitalAshok.

This does currently break namespace foo { inline namespace std {} }, namespace foo::inline std {}, etc.

clang/lib/Sema/SemaDeclCXX.cpp
11396

You need to check if this is a std namespace declaration at file scope, namespace foo::inline std {} in a namespace scope should be fine.

The check for this is a few lines below: CurContext->getRedeclContext()->isTranslationUnit().

11429–11430

This ends up giving two errors on the same line if this wasn't the first declaration (error: namespace 'std' cannot be declared inline followed by error: non-inline namespace cannot be reopened as inline; note: previous definition is here). The wording for the second error is also a little confusing (it cannot be opened at all as inline, let alone reopened), so consider refactoring so that both diagnostics can't be issued at once

This revision now requires changes to proceed.Jul 24 2023, 1:50 AM
rZhBoYao updated this revision to Diff 543514.Jul 24 2023, 6:28 AM
rZhBoYao marked an inline comment as done.

Addressed comments

MitalAshok accepted this revision.Jul 24 2023, 10:27 AM

Looks good!

This revision is now accepted and ready to land.Jul 24 2023, 10:27 AM

Thanks for all the suggestions and review comments!