Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Issue 6905 - ref acts as auto ref when return type is missing #1401

Merged
merged 1 commit into from over 1 year ago

3 participants

Hara Kenji Andrej Mitrovic Walter Bright
Hara Kenji 9rnsr referenced this pull request in D-Programming-Language/phobos
Merged

Additional fix for issue 6905 #1028

Andrej Mitrovic
Collaborator

I think I'm going to write a pull request to update the documentation, because this isn't described anywhere:

ref bar1() { static int n; return n; }

IOW, ref can act as type inference just like auto can, except ref forces lvalue returns, auto forces rvalue returns, and auto ref is automatic.

However I'm not sure if this is deliberately designed this way or just another case of attributes acting as if they're auto. For example Issue 3573 (and there are other duplicate reports like this where a keyword acts as auto).

Hara Kenji
Collaborator

From a long time ago, I have thought it is a feature. All function attributes with no explicit return type introduces return type inference. Certainly, in the early on the D2, Walter might had not intended about that. But I could not find any problems there in thinking of my three years. So, I can say there is no problem.

Andrej Mitrovic
Collaborator

From a long time ago, I have thought it is a feature. All function attributes with no explicit return type introduces return type inference.

I think it only makes sense for ref. If you use const:

const foo() { ... }

If foo is a method, does const apply to the method or the return type? And is foo a void method or a method that returns?

nothrow and pure are also attributes which are confusing because you can't tell if the function returns or not. I think we need to talk with @WalterBright and @andralex and decide exactly which attributes can be used for type inference. I think auto, auto ref, and ref should be the only ones.

Edit: I think const ref and maybe const auto also work, but I'm unsure. But I think they should be allowed if they work. We have so many of these..

Hara Kenji
Collaborator

If foo is a method, does const apply to the method or the return type? And is foo a void method or a method that returns?

const apply to the method. I know there is a kind of confusing, but it is a different issue.

nothrow and pure are also attributes which are confusing because you can't tell if the function returns or not.

All return type inference implies "you can't tell if the function returns or not". It is the purpose.

Or, you are talking about ref + void return? If so, I can agree it is bit weird.

Hara Kenji 9rnsr closed this
Hara Kenji 9rnsr reopened this
Walter Bright WalterBright merged commit f193abd into from
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 24, 2012
k-hara fix Issue 6905 - ref acts as auto ref when return type is missing 2518851
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 32 additions and 3 deletions. Show diff stats Hide diff stats

  1. +3 3 src/statement.c
  2. +29 0 test/runnable/declaration.d
6 src/statement.c
@@ -3919,7 +3919,7 @@ Statement *ReturnStatement::semantic(Scope *sc)
3919 3919 }
3920 3920 else
3921 3921 {
3922   - if (tf->isref)
  3922 + if (tf->isref && (fd->storage_class & STCauto))
3923 3923 { /* Determine "refness" of function return:
3924 3924 * if it's an lvalue, return by ref, else return by value
3925 3925 */
@@ -3932,11 +3932,11 @@ Statement *ReturnStatement::semantic(Scope *sc)
3932 3932 unsigned errors = global.startGagging();
3933 3933 exp->checkEscapeRef();
3934 3934 if (global.endGagging(errors))
3935   - { tf->isref = FALSE; // return by value
3936   - }
  3935 + tf->isref = FALSE; // return by value
3937 3936 }
3938 3937 else
3939 3938 tf->isref = FALSE; // return by value
  3939 + fd->storage_class &= ~STCauto;
3940 3940 }
3941 3941 tf->next = exp->type;
3942 3942 //fd->type = tf->semantic(loc, sc); // Removed with 6902
29 test/runnable/declaration.d
@@ -21,6 +21,34 @@ void test6475()
21 21 }
22 22
23 23 /***************************************************/
  24 +// 6905
  25 +
  26 +void test6905()
  27 +{
  28 + auto foo1() { static int n; return n; }
  29 + auto foo2() { int n; return n; }
  30 + auto foo3() { return 1; }
  31 + static assert(typeof(&foo1).stringof == "int delegate()");
  32 + static assert(typeof(&foo2).stringof == "int delegate()");
  33 + static assert(typeof(&foo3).stringof == "int delegate()");
  34 +
  35 + ref bar1() { static int n; return n; }
  36 + static assert(!__traits(compiles, {
  37 + ref bar2() { int n; return n; }
  38 + }));
  39 + static assert(!__traits(compiles, {
  40 + ref bar3() { return 1; }
  41 + }));
  42 +
  43 + auto ref baz1() { static int n; return n; }
  44 + auto ref baz2() { int n; return n; }
  45 + auto ref baz3() { return 1; }
  46 + static assert(typeof(&baz1).stringof == "int delegate() ref");
  47 + static assert(typeof(&baz2).stringof == "int delegate()");
  48 + static assert(typeof(&baz3).stringof == "int delegate()");
  49 +}
  50 +
  51 +/***************************************************/
24 52 // 7019
25 53
26 54 struct S7019
@@ -169,6 +197,7 @@ void test8942()
169 197 int main()
170 198 {
171 199 test6475();
  200 + test6905();
172 201 test7019();
173 202 test7239();
174 203 test8123();

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.