Skip to content

Commit 45fe392

Browse files
authored
[generator] BG8403 when type name matches enclosing namespace name (#1010)
Fixes: #982 As elaborated upon in [Eric Lippert's blog][0] series "Do not name a class the same as its namespace" ([Part 1][1], [Part 2][2], [Part 3][3], and [Part 4][4]), and as explained in the [Framework Design Guidelines][5]: > ❌ DO NOT use the same name for a namespace and a type in that namespace. > > For example, do not use `Debug` as a namespace name and then also > provide a class named `Debug` in the same namespace. > Several compilers require such types to be fully qualified. emit a BG8403 warning when a type is generated which matches the enclosing namespace "part": namespace Android.Graphics.Drawable { partial class Drawable { } } Such constructs will now emit the warning: warning BG8403: Type `Android.Graphics.Drawable.Drawable` has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information. This construct not a warning in Roslyn, and bindings code is autogenerated, so it can be hard for bindings authors to notice that they are making this mistake. <https://aka.ms/BG8403> explains the issue and possible solutions, which generally includes using [`@(AndroidNamespaceReplacement)`][6] to rename the namespace (e.g. to `Android.Graphics.Drawables`) or using `Metadata.xml` to rename the type. ~~ Testing Note ~~ It is very hard to unit test `generator` warning/error reporting, since the `Report` type is a global static class. Add a static property `Report.OutputDelegate` that allows the tester to temporarily set a different output method that tests can use to verify `Report` output. These tests should be marked `[NonParallelizable]` since they affect the global state shared with any other running tests. [0]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert [1]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-one [2]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-two [3]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-three [4]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-four [5]: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces [6]: dotnet/android@398afd4
1 parent fe60483 commit 45fe392

File tree

20 files changed

+203
-4
lines changed

20 files changed

+203
-4
lines changed

src/Java.Interop.Localization/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Java.Interop.Localization/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ In this message, the term "constants" refers to class or interface members that
201201
{1} - .NET field name.
202202
{2} - Java type.</comment>
203203
</data>
204+
<data name="Generator_BG8403" xml:space="preserve">
205+
<value>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</value>
206+
<comment>{0} - Java type.</comment>
207+
</data>
204208
<data name="Generator_BG8500" xml:space="preserve">
205209
<value>Unexpected child element of '&lt;interface&gt;': '{0}'.</value>
206210
<comment>{0} - XML element name.

src/Java.Interop.Localization/xlf/Resources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.ko.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.pl.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

0 commit comments

Comments
 (0)