Allow nested file-local types #7360
Replies: 5 comments 6 replies
-
You could make the nested type GeneratedProxy_IBar |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I guess the implementation problems here are around "is it the same named-type-symbol when used in different places, or are there now two different and competing named-type-symbol definitions for the same type? what type members does the named-type-symbol advertise in each scenario? what are their advertised names and other metadata?" I don't know enough about Roslyn to opine on whether those are problems that need addressing (or indeed, whether I'm wrong and this isn't the concern at all) |
Beta Was this translation helpful? Give feedback.
-
Hmmm; in this specific case, a valid workaround (from C# 8 onwards, so: precedes // generated code
partial class Foo
{
[Proxy(typeof(GeneratedProxy_IBar))]
public partial interface IBar
{
// this type does not need to be available anywhere other than the attribute
private sealed class GeneratedProxy_IBar : global::Foo.IBar
{
// TODO: do something interesting
void global::Foo.IBar.Blap() => throw new NotImplementedException();
}
}
} which is an improvement (it is still visible / accessible inside |
Beta Was this translation helpful? Give feedback.
-
This is something I want to do eventually, as well as class C
{
file class Inner { }
}
class D : C.Inner { } // is this allowed? |
Beta Was this translation helpful? Give feedback.
-
Summary
Currently, the
file
scoped modifier is limited to top-level types; attempting to use the modifier in a nested type gives CS9054I am ignorant of the reasons for this, but: it creates some problematic scenarios around accessibility.
Consider:
Motivation
where we want a generator to do something like:
We do not intend for
GeneratedProxy_IBar
to be visible or accessible to regular code - it is used only via the attribute at runtime, to skip a ref-emit layer (think: AOT). It would be an ideal candidate forfile
, however:file
where it is currently, we get CS9054 (file-local must be top level)file
and move the type to top-level, we get CS0122 (accessibility)Our only option currently is to leave the type unnecessarily visible to user code. Not insurmountable, but ugly and vexing, especially as it is only the reduced accessibility of
IFoo
that has blocked us from masking the generated type.Workarounds
We can also add a bunch of attributes
[Browsable(false)]
,[EditorBrowsable(EditorBrowsableState.Never)]
,[GeneratedCode("...","...")]
,[DebuggerNonUserCode]
on the generated type, which may help a little, but isn't quite the same as an unspeakable name.Suggestion
Remove the limitation on the
file
modifier to top-level types, since there are scenarios where they may usefully apply to nested types.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions