better-cpp-syntax icon indicating copy to clipboard operation
better-cpp-syntax copied to clipboard

Add trailing return type

Open rianquinn opened this issue 5 years ago • 8 comments

Checklist

  • [x] This problem exists even with the setting "C_Cpp.enhancedColorization": "Disabled"
  • [x] This bug exists for C
  • [x] This bug exists for C++
  • [ ] This bug exists for Objective-C
  • [ ] This bug exists for Objective-C++

The code with a problem is:

Deleter &
get_deleter() noexcept
{
    return m_deleter;
}

const Deleter &
get_deleter() const noexcept
{
    return m_deleter;
}

auto
get_deleter() noexcept -> Deleter &
{
    return m_deleter;
}

auto
get_deleter() const noexcept -> const Deleter &
{
    return m_deleter;
}

It looks like:

** screenshot + theme name preferable ** image

It should look like:

"const" and "noexcept" show different colors based on return type. It appears that trailing returns are not highlighting properly

rianquinn avatar Oct 21 '19 11:10 rianquinn

As shown here, "const" and "noexcept" are not the same color, and "Deleter" changes color as well

rianquinn avatar Oct 21 '19 11:10 rianquinn

This is due to a lack of support for trailing return types.

@jeff-hykin Additionally, keywords should be avoided inside the member access.

matter123 avatar Oct 21 '19 16:10 matter123

Hmmm I thought we had a previous issue about trailing return types but I can't find it.

Sadly @rianquinn , as Matter123 said, we haven't worked on trailing return types yet so all highlighting for them (if any) is accidental. Current highlighting is probably from pointer access e.g.this->member_var

jeff-hykin avatar Oct 22 '19 14:10 jeff-hykin

additionally (just fyi) having auto on the line-before and the { on the line after would make it nearly impossible for the parser to know get_deleter() was a function-definition and not a function call, which would change how the trailing return type was parsed even once we do have support.

jeff-hykin avatar Oct 22 '19 14:10 jeff-hykin

A trailing return type is the only valid syntax that looks like that in the function head.

More precisely a line that starts with foo() can only be a function call when inside of a method context. Therefore outside of a method context any line that has the form foo() -> bar must contain a trailing return type.

@jeff-hykin I think the grammar can be more aggressive in asserting that its a function definition rather than a variable declaration when a trailing return type is present (https://github.com/jeff-hykin/cpp-textmate-grammar/issues/198)

std::string s("comment") -> bar should never be valid.

matter123 avatar Oct 22 '19 15:10 matter123

Previous discussion https://github.com/jeff-hykin/cpp-textmate-grammar/issues/371

matter123 avatar Oct 22 '19 16:10 matter123

Care needs to be taken about user-defined template deduction guides.

template<class C>
A(C b) -> typename C::type

Though I am not sure that there actually is a difference un how syntax highlighting would be performed.

matter123 avatar Oct 22 '19 18:10 matter123