Commit 45fe392
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@398afd41 parent fe60483 commit 45fe392
File tree
20 files changed
+203
-4
lines changed- src
- Java.Interop.Localization
- xlf
- Java.Interop.Tools.Generator/Utilities
- tests/generator-Tests/Unit-Tests
- tools/generator
- Extensions
- Java.Interop.Tools.Generator.CodeGeneration
- SourceWriters/Extensions
20 files changed
+203
-4
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
204 | 208 | | |
205 | 209 | | |
206 | 210 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
0 commit comments