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

[DoesNotReturn] warning is only reported in nullable-enabled context #5231

Closed
cston opened this issue Sep 24, 2021 · 15 comments
Closed

[DoesNotReturn] warning is only reported in nullable-enabled context #5231

cston opened this issue Sep 24, 2021 · 15 comments
Labels

Comments

@cston
Copy link
Member

cston commented Sep 24, 2021

Warning CS8763 should be reported for F1() below:

using System.Diagnostics.CodeAnalysis;

class Program
{
    [DoesNotReturn] static void F1() { } // no warning

#nullable enable
    [DoesNotReturn] static void F2() { } // warning CS8763
}

namespace System.Diagnostics.CodeAnalysis
{
    public class DoesNotReturnAttribute : Attribute { }
}
@RikkiGibson
Copy link
Contributor

I think the behavior in the issue description is expected. DoesNotReturn is not respected by control flow analysis, definite assignment, etc. Is there some design notes/documentation indicating otherwise?

@jcouv jcouv transferred this issue from dotnet/roslyn Sep 28, 2021
@jcouv
Copy link
Member

jcouv commented Sep 28, 2021

Moved the issue to csharplang since it's a language design question.

@jcouv jcouv added the Proposal label Sep 28, 2021
@jcouv
Copy link
Member

jcouv commented Sep 28, 2021

Added topic to LDM backlog (when convenient)

@333fred
Copy link
Member

333fred commented Sep 28, 2021

@jcouv like Rikki said, I don't understand what we're trying to ask here. This is the expected behavior of all nullable flow analysis attributes: what else are you expecting to see?

@jcouv
Copy link
Member

jcouv commented Sep 28, 2021

@333fred Yes, the current behavior is by current design. The ask is to change this design ;-)

The proposed change and expectation are described in OP. Unlike other nullability attributes, this one doesn't have anything to do with nullability.
If we explore this, there are some other additions we should discuss too (effect on definite assignment). For example, I think this warning would fall out from this proposal:

F1();
int i = 0; // warning CS0162: Unreachable code detected

@RikkiGibson
Copy link
Contributor

This seems about equally sound as definite assignment of out parameters. There's no way to guarantee that a method with an out parameter from metadata never reads the argument before assigning it, or that the method assigns it at all.

void M()
{
    int x;
    SomeLibrary.M(out x);
    x.ToString(); // could still be uninitialized?
}

@333fred
Copy link
Member

333fred commented Sep 29, 2021

Unlike other nullability attributes, this one doesn't have anything to do with nullability

It only has to do with nullability. No other part of the language or compiler pays attention to any of these attributes. It does not affect definite assignment in any fashion. I guess I'm not sure what has changed in the past 2 years that we feel we need to revisit this again.

@333fred
Copy link
Member

333fred commented Oct 2, 2021

@jcouv did you have a response to my question above?

@jcouv
Copy link
Member

jcouv commented Oct 2, 2021

@333fred It's a question for Chuck who's raising the issue. Personally, I am sympathetic to the proposal and simply having more experience with the attributes is a sufficient reason to revisit.

@cston
Copy link
Member Author

cston commented Oct 4, 2021

It only has to do with nullability.

The attribute indicates only that the method does not return which seems independent of nullability, and the warning "CS8763: A method marked [DoesNotReturn] should not return" does not mention nullability, so it's surprising the warning is only reported in a #nullable enable context.

@Youssef1313
Copy link
Member

I can see this both ways.

if (x is null)
{
    ADoesNotReturnMethod();
}

_= x.ToString() // no warning. This is the original intent of the attribute.

However, from another perspective, it's just a "DoesNotReturn" where I want the compiler to warn me if it's used while the method does return. So whether or not the compiler is using it for nullability analysis is just one side of it.

@RikkiGibson
Copy link
Contributor

Are we comfortable introducing a new error in implementations when DoesNotReturn is applied, but the method does return? That's what would be necessary to meet "definite assignment" standards, but the shipped behavior is only to warn.

@jcouv
Copy link
Member

jcouv commented Oct 13, 2021

Discussed in LDM 10/13/2021: in short, keep as-is

@jcouv jcouv closed this as completed Oct 13, 2021
@CyrusNajmabadi
Copy link
Member

Discussed in LDM 10/13/2021: in short, keep as-is

In long: we are more interested in investing our efforts into a more powerful, generalized, solution like never/bottom types: #538

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

6 participants