Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should sizeof error message recommend Marshal.SizeOf? #26513

Closed
svick opened this issue Apr 30, 2018 · 3 comments
Closed

Should sizeof error message recommend Marshal.SizeOf? #26513

svick opened this issue Apr 30, 2018 · 3 comments
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Milestone

Comments

@svick
Copy link
Contributor

svick commented Apr 30, 2018

Version Used: Roslyn 2.7.0 or master

Steps to Reproduce:

Try to compile the following code:

struct S
{
    int SizeOfS() => sizeof(S);
}

Actual Behavior:

error CS0233: 'S' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)

Expected Behavior:

Since Marshal.SizeOf() is not a direct replacement for sizeof(), maybe the error message should not mention it? On .Net Core, it could make sense to suggest Unsafe.SizeOf() instead, but that method is not available on .Net Framework or .Net Standard out of the box (it requires the System.Runtime.CompilerServices.Unsafe package), so maybe that suggestion should be removed altogether?

@jcouv jcouv added Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. labels May 3, 2018
@jcouv jcouv added this to the 16.0 milestone May 3, 2018
@jaredpar
Copy link
Member

Rather than this error we should just allow sizeof in any context. There is really no reason to have this be unsafe only other than accident of history. I think we should just file a bug on csharplang to do this.

@svick
Copy link
Contributor Author

svick commented Aug 31, 2018

@jaredpar There are already issues for allowing sizeof for generic types constrained to struct dotnet/csharplang#1032 and to unmanaged dotnet/csharplang#1508. I didn't find one for always allowing it.

@jaredpar
Copy link
Member

@svick i saw the same when I searched. I think we need a new issue to cover just blanket allowing it. I was surprised we didn't have on already.

dotnet/csharplang#1828

eerhardt added a commit to eerhardt/roslyn that referenced this issue Feb 27, 2019
Marshal.SizeOf is not a direct replacement for `sizeof`.

Fix dotnet#26513
agocke pushed a commit that referenced this issue Feb 28, 2019
Marshal.SizeOf is not a direct replacement for `sizeof` and the issues around it are subtle.

Fix #26513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Projects
None yet
Development

No branches or pull requests

3 participants