Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

How to annotate an owner properly? #97

Open
brevzin opened this issue Jan 28, 2021 · 5 comments
Open

How to annotate an owner properly? #97

brevzin opened this issue Jan 28, 2021 · 5 comments
Labels
bug Something isn't working false-negative

Comments

@brevzin
Copy link

brevzin commented Jan 28, 2021

Here's a simple example:

#include <vector>
#include <string>

void use(char);
auto f() -> std::vector<std::string>;

int main() {
    for (char c : f()[1]) {
        use(c);
    }
}

This, correctly and awesomely, issues a warning on the for loop because of the object being destroyed. Cool.

Now let's say I have my own custom vector of strings, which I'm calling VS because I'm not creative:

#include <vector>
#include <string>

void use(char);

struct [[gsl::Owner]] VS {
    auto operator[](size_t i) -> std::string&;
    auto begin() -> std::vector<std::string>::iterator;
    auto end() -> std::vector<std::string>::iterator;

    std::vector<std::string> v;
};

auto f() -> VS;

int main() {
    for (char c : f()[1]) {
        use(c);
    }
}

This does not warn, even though it's basically the same example and I tried to annotate VS as being an owner. Is there some way to still get a warning for such code?

Obligatory compiler explorer link.

@mgehre
Copy link
Owner

mgehre commented Feb 1, 2021

This seems to be an issue with determining the pointee type of the Owner VS.
Adding some debugging and an extra member makes it correctly deduce the pointee type, see https://godbolt.org/z/Erb77o

I thought that [[gsl::Owner(std::string)]] was a way to manually specify that, but it didn't work.
Also declaring operator[] is supposed to be enough to correctly determine the pointee type.

I'll leave this bug open until that is fixed.

@mgehre mgehre added bug Something isn't working false-negative labels Feb 1, 2021
@brevzin
Copy link
Author

brevzin commented Feb 1, 2021

Also declaring operator[] is supposed to be enough to correctly determine the pointee type.

I think if the type is a range (has begin/end), you should be able to use that too?

@mgehre
Copy link
Owner

mgehre commented Feb 4, 2021

@mgehre
Copy link
Owner

mgehre commented Feb 4, 2021

For some reason, it doesn't recognize std::vector<std::string>::iterator as Pointer.

@mgehre
Copy link
Owner

mgehre commented Feb 4, 2021

It seems to be an issue when std::vector<std::string>::begin is not used and thus not instantiated, see
https://godbolt.org/z/6bbqKT

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working false-negative
Projects
None yet
Development

No branches or pull requests

2 participants