One of these design principles (modular features) allows directories to have their own namespace. There is currently guidance from cxx@ that disallows namespaces in //chrome. I attempted to remove this guidance, but was informed that I first need consensus from this mailing list.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/5eceeded-593e-485c-a68e-baf87e8ab5fcn%40chromium.org.
The fundamental problem for building namespaces out of packages is that we rarely rely on fully-qualified lookup in C++, normally writing
std::unique_ptr
rather than::std::unique_ptr
. Coupled with lookup in enclosing namespaces, this means that for code in a deeply nested package (::division::section::team::subteam::project
, for example) any symbol that is not fully qualified (std::unique_ptr
) can in fact reference any of
::std::unique_ptr
::division::std::unique_ptr
::division::section::std::unique_ptr
::division::section::team::std::unique_ptr
::division::section::team::subteam::std::unique_ptr
::division::section::team::subteam::project::std::unique_ptr
Practically speaking, the following is the best we can do given the realities of most codebases:
- Have a database of some form for a codebase to identify the unique namespaces.
- When introducing a new namespace, use that database and introduce it as a top-level.
- If for some reason the above is impossible, never ever introduce a sub-namespace that matches a well-known top-level namespace. No sub-namespaces for
absl
,testing
,util
, etc. Try to give sub-namespaces unique names that are unlikely to collide with future top-levels.- When declaring namespace aliases and using-declarations, use fully qualified names, unless you are referring to a name inside the current namespace, as per TotW 119.
- For code in
util
or other commonly-abused namespaces, try to avoid full qualification, but qualify if necessary.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaSvkwgcfW%3DCK9F-zJpKO9KBiVXh0Nj534FhvFF2rXB22g%40mail.gmail.com.
The current Google3 guidance (https://abseil.io/tips/130) actually recommends against namespace nesting and against naming namespaces based on folder paths.
I'm happy to take a VC.
* Based on your questions and comments above, it seems like you're interested in a machine-automatable, not-too-verbose, conflict-free mechanism for defining namespaces. Since no one has been able to come up with a system to do so, the default guidance until now is to not use namespaces.
If we do share a namespace across //chrome/browser/<foo> and //components/<foo>, then I expect this to be the same feature, where //components/<foo> is the x-platform impl, and //chrome/browser/<foo> is the platform-specific glue. Using the same namespace is desired.
On Friday, May 10, 2024 at 7:20:48 AM UTC-7 Jean-Philippe Gravel wrote:The current Google3 guidance (https://abseil.io/tips/130) actually recommends against namespace nesting and against naming namespaces based on folder paths.Thanks for this! I had re-skimmed the "namespace names" section of the Google Style Guide last night, but somehow didn't absorb its link to this or the impact of the recommendations here.This guidance makes sense to me (and, if we complied, would alleviate a number of clashes I've seen in my local jumbo build experiments). And it would allow for more namespacing, which I think would be nice.What we don't have (that it recommends, and I think we need) is a database of namespaces used in the project, so we can avoid clashes (and people can figure out what corresponds to what). Is there existing best practice somewhere we can use? Should we just have a text file somewhere with a big list?
If the folks on this list want to put together global guidance for the chromium repository on how to deal with namespaces, please do so!Right now there is guidance under styleguide/c++ that prohibits namespaces, but only in //chrome. Given that //chrome/OWNERS want to allow namespaces, and there are already plenty of examples of namespaces in //chrome, can we remove this guidance from styleguide/c++?
I think we should rewrite that guidance to say that we don't use a `chrome` namespace for all of //chrome, we put the core of it in the global namespace, with some wordsmithing applied.
I'm not an expert on either. Do you have a link to previous discussions on the problems with namespace collision/shared namespaces and modules/Rust?
> Most code should be in a namespace, with the exception of code under //chrome, which may be in the global namespace (do not use the `chrome::` namespace).Is this to accommodate existing code, or is this really the ideal we want to promote going forward? Global symbols are dangerous because they can conflict with symbols having the same name in any namespace anywhere in the code base.
> if a nested namespace is needed, do not reuse the name of any top-level namespaceSimply not reusing existing top-level namespace might not be sufficient. The TOTW says: "Try to give sub-namespaces unique names that are unlikely to collide with future top-levels." That is, we should choose names that are unique right now, and that are specific enough to not conflict with new namespaces created in the future. The TOTW also calls out: "No sub-namespaces for absl, testing, util, etc." These namespace names are bad because they are very likely to create conflicts. You could easily end up with a situation like: `::util`, `::foo::util`, `::foo::bar::util`. Should we include these in our recommendations?
I'm not an expert on either. Do you have a link to previous discussions on the problems with namespace collision/shared namespaces and modules/Rust?
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFAd_jnfkyxZ_3fk5POiKyaAsKBX-0FeRZQfAFgg4%2BFV0A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAC_ixdxm4uNqyGYBSF3VVUxPA38utzpkkys%2BpDiZXibxX2%3D_Hg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAKQnr0QO%2BZ4oEC62XTn37ffTr4AreySxs-%3D8M8Here5brXfxUw%40mail.gmail.com.
Maybe explicitly mention that nested "internal" namespaces are commonly used for implementation details and are okay to use when it is not possible to keep the implementation details out of the header file?
I'm concerned about the manual step of consulting/editing a //NAMESPACES file. It seems like one more thing that reviewers need to add to their list of things to check. Unless, of course, it's automated via presubmit/tricium/somesuch. Is that practical?
For the issue of well-defined boundaries, I think good structure in the GN files and prolific use of `public` rather than lumping everything in `sources` is even better than namespaces.
I'm not sure that it should matter since it's just an opinion, but here's mine: I have repeatedly thought "how is this not just noise?" when browsing code in //chrome that uses namespaces.
I don't understand the mechanism or rationale for "Record namespace use in //NAMESPACES, to avoid overlap"Is this going to be an ordered list?
If I want to use "namespace foo" and "foo" is already in //NAMESPACES, does that mean I can use it because it's already in use, or that I shouldn't use it because it's already in use?
Does this include namespaces from third-party libraries?
Is there precedence for trying to enumerate all namespaces?
I asked this earlier but maybe I missed the answer: what is the problem with namespace collision? The earlier response was "The primary reason not to do that was modules and Rust. Do you have reason to believe this won't be problematic for either of those?"
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/05a69193-5dc7-42fc-b85d-156a488d5561n%40chromium.org.
How should //NAMESPACES work with regards to //chrome vs //ios/chrome? Can we reuse the same namespace across both?
Should they be exclusive (i.e. should namespace in //ios/chrome use an ios_ prefix)?
With regards to sharing namespace between //components and //chrome and //ios/chrome, I know that lots of engineers are currently choosing to reuse the same namespace for their feature in //components/$feature and the embedder specific part in //ios/chrome/browser/$feature. If I understand the proposal correctly, this is not something that should be done.
If I want to use "namespace foo" and "foo" is already in //NAMESPACES, does that mean I can use it because it's already in use, or that I shouldn't use it because it's already in use?
Thanks Peter for the link. It seems like the concern is not about namespace collision, but nested namespace collision. For that reason the google style guide recommends against namespace nesting. I don't think we need to restate this in our derived style guide.
If I want to use "namespace foo" and "foo" is already in //NAMESPACES, does that mean I can use it because it's already in use, or that I shouldn't use it because it's already in use?> If the "foo" is for an unrelated purpose, then you can't use it, unless you rename existing usage.I think you missed my question. How does the developer tell if "foo" is for an unrelated purpose, other than manually auditing all code that uses "namespace foo", which seems like a big ask?
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAEYHnr2S8u3PoAZt4jbxweg04OAf9%2BU8KfB4bveb%3DfntinA3oA%40mail.gmail.com.
For what it's worth I think the point of the whole "you should have a database" recommendation is that namespaces should be "owned" in the sense that for any given top-level namespace you should have some person/group/project which is responsible for deciding what gets to go into it. The database then just follows from that in the sense that it's probably a good idea to have a straightforward way to track down if a name is in use or not, and which persons are responsible for it.This doesn't necessarily require something particularly strict, Google's internal system isn't even enforced and basically just runs on the expectation that everyone is trying to operate in good faith, as far as I can tell.
(I'm not trying to argue for or against //NAMESPACES here, I just thought this might be some helpful context?)--On Wed, May 22, 2024 at 9:38 PM Erik Chen <erik...@chromium.org> wrote:Going back to Peter's original proposal:
"Most code should be in a namespace, with the exception of code under //chrome, which may be in the global namespace (do not use the `chrome::` namespace). Record namespace use in //NAMESPACES, to avoid overlap. Minimize use of nested namespaces, as they do not actually improve encapsulation; if a nested namespace is needed, do not reuse the name of any top-level namespace. For more detailed guidance and rationale, see https://abseil.io/tips/130."
The first and 3rd parts LGTM. I don't think //NAMESPACES will provide much utility (unless we also target 3rd-party libraries, which opens its own can of worms). That being said if it's automated I also don't think it will add much cost. I'll defer to people who have stronger opinions.--On Thu, May 23, 2024 at 12:57 PM Peter Kasting <pkas...@chromium.org> wrote:On Wednesday, May 22, 2024 at 2:03:40 PM UTC-7 Erik Chen wrote:Thanks Peter for the link. It seems like the concern is not about namespace collision, but nested namespace collision. For that reason the google style guide recommends against namespace nesting. I don't think we need to restate this in our derived style guide.The section you link to doesn't recommend against nesting (though it does recommend against "overly deep" nesting). That more stringent recommendation is only in the ToTW, so it wouldn't necessarily apply to us unless we restated it. (Also, given the pervasiveness of violations of this in our codebase, clearly I'm not the only one who's been mistaken about the style guidance for years, so explicitly restating it seems good anyway.)Top-level namespace collision is more obviously bad, so there's less need to explicitly address it. It seems clear to me, though, that if completely unrelated subprojects use the same top-level namespace, things are ripe for both misunderstanding and symbol conflicts, and we want to avoid that.Also keep in mind that this text is intended for the "Dos And Don'ts" page, which, while it should be cited in reviews and considered "best practice", does not carry quite the mandatory heft of the full style guide -- if author and reviewer agree to bypass it, so be it.If I want to use "namespace foo" and "foo" is already in //NAMESPACES, does that mean I can use it because it's already in use, or that I shouldn't use it because it's already in use?> If the "foo" is for an unrelated purpose, then you can't use it, unless you rename existing usage.I think you missed my question. How does the developer tell if "foo" is for an unrelated purpose, other than manually auditing all code that uses "namespace foo", which seems like a big ask?Because I intend to list the associated directories for each entry in //NAMESPACES, and have the presubmit check that new usage is in a descendant of one. (I did actually think all this through beforehand but assumed it would be clear when the file was constructed and that this list's concern would be whether we should comply with Google's recommendations at all, rather than wanting the details. Mea culpa.)PK
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAEYHnr2S8u3PoAZt4jbxweg04OAf9%2BU8KfB4bveb%3DfntinA3oA%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAAE6ASDPy2_o44W0MZ3S3voq622Qjr2vSD_VqFh0-Dr2xpwGA%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/2971daab-d9b1-49ce-b40a-2ff6c294fb5an%40chromium.org.
sounds like the right path forward is that we immediately land the wording change without the sentence on //NAMESPACES, I eventually attempt to write a patch to add such a file + appropriate PRESUBMIT checks to see how feasible it is, and if the PRESUBMIT owners are OK with that patch, we go ahead and add it and the relevant language at that point.