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

member function void foo() __restrict__; incorrectly applies restrict to type, not pointer #18121

Closed
silvasean opened this issue Oct 31, 2013 · 5 comments · Fixed by #83187
Closed
Labels
bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@silvasean
Copy link
Contributor

silvasean commented Oct 31, 2013

Bugzilla Link 17747
Version trunk
OS Linux
CC @majnemer,@DougGregor,@imallett,@rnk

Extended Description

struct Foo {
  void member() __restrict__ {
    Foo *__restrict__ This = this;
  }
};

Clang gives the diagnostic:

test.cpp:3:23: error: cannot initialize a variable of type 'Foo *restrict' with
      an rvalue of type 'restrict Foo *'
    Foo *__restrict__ This = this;
                      ^      ~~~~
1 error generated.

Clang seems to think that this has type restrict Foo * (restrict on Foo, rather than the pointer)

The GCC extension says:

http://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html#Restricted-Pointers

"Within the body of T::fn, this has the effective definition T *restrict const this."

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Nov 4, 2013

*** Bug llvm/llvm-bugzilla-archive#17793 has been marked as a duplicate of this bug. ***

@rnk
Copy link
Collaborator

rnk commented Feb 26, 2018

I ran into this again while working on debug info, and wrote up a long bug report to discover that it was a duplicate.

See also #11039 .

@imallett
Copy link
Mannequin

imallett mannequin commented Oct 19, 2020

I also ran into this bug, and also wrote up a long bug report to discover that it was a duplicate. So, I'll try to add a bit more information. This bug is almost 7 years old now!

So first, this still occurs in Clang trunk 12.0.0 (but way earlier also; I checked as far back as 3.0.0). I can't give a cleaner example than the OP, but I will comment that Clang's error message is, specifically:

<source>:3:23: error: cannot initialize a variable of type 'Foo *__restrict' with an rvalue of type '__restrict Foo *'
    Foo *__restrict__ This = this;
                      ^      ~~~~

It is also worth noting the GCC, MSVC, ICC, and possibly others all do not suffer from this problem.

As has been mentioned, the bug is that Clang appears to think that the type of this is Foo __restrict__* (that is, a pointer to a restrict-qualified Foo object, not a restrict-qualified pointer to a Foo object). This, of course, makes no sense. Restrict-qualification is a pointer thing. Indeed, in other contexts, even writing such a type is sensibly impossible. E.g. the following line will correctly fail:

Foo __restrict* foo = nullptr; //fails (correctly)

Note: while __restrict is not a C++ keyword, when applied to C++ member functions, it is widely implemented as the this pointer becoming restrict-qualified. That seems to be what Clang attempted here, but got the type incorrect.

Note: a C-style cast works around this problem, but obviously this is not a solution.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Nov 26, 2021

mentioned in issue llvm/llvm-bugzilla-archive#17793

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 9, 2021
Sirraide added a commit that referenced this issue Feb 29, 2024
…ns properly (#83187)

When resolving the type of `this` inside a member function, we were
attaching all qualifiers present on the member function to the class
type and then making it a pointer; however, `__restrict`, unlike `const`
and `volatile`, needs to be attached to the pointer type rather than the
pointee type.

This fixes #82941, #42411, and #18121.
@EugeneZelenko EugeneZelenko added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Feb 29, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 29, 2024

@llvm/issue-subscribers-clang-frontend

Author: Sean Silva (silvasean)

| | | | --- | --- | | Bugzilla Link | [17747](https://llvm.org/bz17747) | | Version | trunk | | OS | Linux | | CC | @majnemer,@DougGregor,@imallett,@rnk |

Extended Description

struct Foo {
void member() restrict {
Foo *restrict This = this;
}
};

Clang gives the diagnostic:

test.cpp:3:23: error: cannot initialize a variable of type 'Foo *restrict' with
an rvalue of type 'restrict Foo *'
Foo *restrict This = this;
^ ~~~~
1 error generated.

Clang seems to think that this has type restrict Foo * (restrict on Foo, rather than the pointer)

The GCC extension says:

<http://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html#Restricted-Pointers>

"Within the body of T::fn, this has the effective definition T *restrict const this."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants