Skip to content

Cannot declare a generic fn with a constraint containing a region #8579

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
erickt opened this issue Aug 17, 2013 · 3 comments
Closed

Cannot declare a generic fn with a constraint containing a region #8579

erickt opened this issue Aug 17, 2013 · 3 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@erickt
Copy link
Contributor

erickt commented Aug 17, 2013

We cannot declare a generic function where one of the tyvars is constrained by a type that contains a region. Here's an example:

trait Trait<'self> {
    fn foo(&self) -> &'self str;
}

fn foo<'a, T: Trait<'a>>(_x: T) {}

fn main() {}

This errors with:

a.rs:5:22: 5:24 error: Illegal lifetime 'a: only 'self is allowed as part of a type declaration
a.rs:5 fn foo<'a, T: Trait<'a>>(_x: T) {}

There is a workaround though. Instead we can use a vtable, which compiles fine.

trait Trait<'self> {
    fn foo(&self) -> &'self str;
}

fn foo<'a>(_x: &Trait<'a>) {}

fn main() {}

@erickt
Copy link
Contributor Author

erickt commented Aug 17, 2013

After poking around a bit in collect.rs, I suspect the problem is in instantiate_trait_ref on this line. Bound traits use the type_rscope, which only permits the 'self region. Instead I think it should probably use a MethodRscope-like scope to allow the bounds to use any user-declared regions. However, there are a couple different users of instantiate_trait_ref so I'm not sure if this is the right thing to do in all cases.

@nikomatsakis: you wrote most of the region code. Is this the right way to solve this bug?

@huonw
Copy link
Member

huonw commented Aug 24, 2013

Similar to #6551 (which is the ICE one gets if one does what it asks and uses foo<T: Trait<'self>> or foo<'self, T: Trait<'self>>).

brandonson added a commit to brandonson/rust that referenced this issue Aug 24, 2013
Unfortunately, due to rust-lang#8579 and rust-lang#6551, extra::workcache can't be made to
work with the new extra::json.  Either ICEs are cause (which is the
state currently) or there are unconstrained types at the callsites for
extra::work_cache::json_encode.
@alexcrichton
Copy link
Member

This is working today due to many changes to lifetimes, and I've seen tests for this in the testsuite so closing.

flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 24, 2022
`unnecessary_join` lint

changelog: Adds a lint called ``[`unnecessary_join`]`` that detects cases of `.collect::<Vec<String>>.join("")` or `.collect::<Vec<_>>.join("")` on an iterator, suggesting `.collect::<String>()` instead

Fixes: rust-lang/rust-clippy#8570

This is a reopen of rust-lang/rust-clippy#8573

changelog: add lint [`unnecessary_join`]
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

3 participants