[RFC PATCH] C++0x trailing return type


I'm just getting started looking at clang and did this for learning
purposes (and fun), so apologies if it's horrible. :wink:

The attached patch (partly) implements the trailing return type
function declaration syntax from C++0x. I am most likely missing
something, so comments welcome.


trailing-return.patch (24.6 KB)

Very nice! One important thing, though, is that the trailing return type is really a property of the declaration, not of the type. In particular, you made type uniquing dependent on whether the return type is trailing, which is incorrect:
int f();
auto f() -> int;
declare the same function; the type is the same.


Accidentally left out the list cc in my first response:

trailing-return-2.patch (14.7 KB)

This looks great! One comment and a request, then I'd like to see this go in:

diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp
index aa30b5c..a4e5392 100644
--- a/lib/Sema/SemaType.cpp
+++ b/lib/Sema/SemaType.cpp
@@ -993,7 +993,31 @@ TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,

I'm attaching a patch that does that.

This looks great! One comment and a request, then I'd like to see this go in:


+ else if (FTI.TrailingReturnType) {
+ Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
+ diag::err_trailing_return_without_auto)
+ << FixItHint::CreateReplacement(D.getDeclSpec().getSourceRange(), "auto");
+ D.setInvalidType(true);
+ }
+ }

I like the use of the Fix-it hint, but I don't think that D.DeclSpec().getSourceRange() is always going to be the correct range. For example, I'd expect that we'd end up fixing

   virtual int f\(int\) \-&gt; float;


   auto f\(int\) \-&gt; float;

because the DeclSpec also contains function specifiers, storage specifiers, etc. It would be nice to have a test in test/FixIt that tests this kind of rewriting, to make sure we got it right.

Yes, I don't know why I thought it would be that simple. I think maybe
the replacement hint wasn't such a good idea in the first place. The
type name can span several files, function and storage specifiers can
occur inside the type source range, and it's just guesswork which of
the return types the user intended. It turned out very messy. I'm
attaching a patch with the __has_feature() support added, but with the
fixit-hint removed. Maybe if the feature really is desirable it can be
added later?

trailing-return-3.patch (16.5 KB)

Sure. I've committed your patch, with a few tweaks, in Clang r115322. Thanks!

  - Doug