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

"no region bound is allowed" errors unless impl mismatches struct defn #6915

Closed
pnkfelix opened this issue Jun 3, 2013 · 8 comments
Closed
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jun 3, 2013

In the below, note the following:

  • If you leave out the impls (so that you compile struct definitions alone), it all compiles correctly.
  • If you include the showbug1 impl, it errors, even though (I think) the impl syntax is (properly) matching the struct definition for B
  • If you instead include the showbug2 impl, it does not error, even though the impl B<int> does not match B<'self, T>, in my opinion.
// foo.rs

pub struct A<'self, T> {
    k : int,
    state: &'self str
}

pub struct B<'self, T> {
    k : int,
}

impl<'self> A<'self, int> { pub fn foo() -> int { 3 } }

#[cfg(showbug1)]
impl<'self> B<'self, int> { pub fn foo() -> int { 4 } }

#[cfg(showbug2)]
impl<'self> B<int> { pub fn foo() -> int { 5 } }

fn main() { }

Transcript:

% rustc  /tmp/foo.rs
warning: no debug symbols in executable (-arch x86_64)
% rustc --cfg showbug1 /tmp/foo.rs
/tmp/foo.rs:13:12: 13:26 error: no region bound is allowed on `B`, which is not declared as containing region pointers
/tmp/foo.rs:13 impl<'self> B<'self, int> { pub fn foo() -> int { 4 } }
                           ^~~~~~~~~~~~~~
error: aborting due to previous error
% rustc --cfg showbug2 /tmp/foo.rs
warning: no debug symbols in executable (-arch x86_64)

To be honest, I don't understand why having a region bound is being disallowed in these cases; I can certainly understand a lint warning about having such a error. But in general, I may want to have macros that expand into impls that insert region bounds, and I may sometimes want to plug in struct names into such macros that do not have region pointers.

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 3, 2013

@nikomatsakis I suspect you can enlighten me about some of the details above.

@msullivan
Copy link
Contributor

So, it seems like what is going on is that the region parameterization is lost if it isn't used. Interesting.

@bblum
Copy link
Contributor

bblum commented Jul 29, 2013

Oops, I was about to comment that this is "working as intended", but then I saw that 'self is phantom on B, and is forgotten about. Definitely a bug.

Also, I thought that the showbug2 impl was illegal, but apparently it isn't? I remember somebody on irc having trouble being able to write a fn foo(&'self self) -> &'self somethingelse function on a non-regioned struct. But evidently that's NOTABUG.

@bblum
Copy link
Contributor

bblum commented Jul 29, 2013

I don't think this is a backward-compatibility hazard. Nominating well-covered.

@nikomatsakis
Copy link
Contributor

The reason for this is because our treatment of lifetimes (particularly those declared on types) is still half-way between the old, inferred world and the new, declared world. More specifically, we still do the inference, and the type system only considers its results, it doesn't really look at what you declared. (@msullivan this is what I was hoping you could fix with the remaining time :) )

Another case where this crops up is a type like:

fn<'a>(fn(x: &'a T))

which is in fact treated by our system currently as:

fn(fn<'a>(x: &'a T))

because that is what the old inference would have decided.

@catamorphism
Copy link
Contributor

Sub-bug of #4846. Just a bug, de-nominating

@eminence
Copy link
Contributor

Seems to be fixed in latest master (dc48adc)

When running the original code example, the showbug1 example compiles without error, and the showbug2 example has a proper error message:

$ rustc --cfg showbug2 a.rs
a.rs:18:10: 18:16 error: wrong number of lifetime parameters: expected 1 but found 0
a.rs:18 impl<'a> B<int> { pub fn foo() -> int { 5 } }
                 ^~~~~~
error: aborting due to previous error

@steveklabnik
Copy link
Member

When changing the sample to use 'a instead of 'self, I see the same results as @eminence . So I think this can be closed, as well.

flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 25, 2021
Do not show docs link when lint doesn't start with "clippy::"

This small change ensures that if the diagnostic functions are called from outside of Clippy, a docs link is not displayed.

---

*Please write a short comment explaining your change (or "none" for internal only changes)*
changelog: restrict docs links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

8 participants