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

Trait constraints should allow anonymous lifetimes for references. #13840

Closed
ciphergoth opened this issue Apr 29, 2014 · 10 comments
Closed

Trait constraints should allow anonymous lifetimes for references. #13840

ciphergoth opened this issue Apr 29, 2014 · 10 comments
Labels
A-lifetimes Area: Lifetimes / regions T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@ciphergoth
Copy link
Contributor

This compiles:

fn isum<'a, I: Iterator<&'a int>>(mut it: I) -> int { it.fold(0, |a, b| a + *b) }

This should be the same, but doesn't compile:

fn isum<I: Iterator<&int>>(mut it: I) -> int { it.fold(0, |a, b| a + *b)}

The compiler complains of a "missing lifetime specifier" at the &int.

@eddyb
Copy link
Member

eddyb commented Apr 29, 2014

cc @nikomatsakis @pnkfelix

@steveklabnik steveklabnik added the A-lifetimes Area: Lifetimes / regions label Jan 23, 2015
@steveklabnik
Copy link
Member

/cc @rust-lang/lang , is this a bug? I would expect a lifetime specifier to be needed here. Updated code. Works:

fn isum<'a, I: Iterator<Item=&'a i32>>(it: I) -> i32 { it.fold(0, |a, b| a + *b)}

doesn't

fn isum<I: Iterator<Item=&i32>>(it: I) -> i32 { it.fold(0, |a, b| a + *b)}

@ciphergoth
Copy link
Contributor Author

I had expected leaving out the lifetime to be the same as supplying an anonymous one, possibly in error.

@steveklabnik
Copy link
Member

The only time you can leave out lifetimes is following the elision rules: https://doc.rust-lang.org/book/lifetimes.html#lifetime-elision and this signature isn't compatible with any of them.

@eddyb
Copy link
Member

eddyb commented Nov 5, 2015

@steveklabnik In a way, this is a feature request.
Of course it doesn't work right now, this is what the issue is about.
Traits generic over lifetimes didn't work for the longest time, and their ergonomics were not improved once they were fixed to actually work.

@steveklabnik
Copy link
Member

Of course it doesn't work right now, this is what the issue is about.

Well, adding more rules for elision would take an RFC these days. We don't keep feature requests in this repository. I've moved it over: rust-lang/rfcs#1352

@ciphergoth
Copy link
Contributor Author

Makes sense. Thanks!

@steveklabnik
Copy link
Member

No problem, thanks for bringing it up! Sorry I was confused about the bug/feature split.

@ciphergoth
Copy link
Contributor Author

I think the confusion was mine, thanks for sorting it out!

@nikomatsakis
Copy link
Contributor

(I personally would like this to work eventually, but we currently do not support it, so I think this is "not a bug".)

@steveklabnik steveklabnik added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Mar 24, 2017
arcnmx pushed a commit to arcnmx/rust that referenced this issue Jan 9, 2023
…undvars, r=lowr

fix: handle lifetime variables in `CallableSig` query

Fixes rust-lang#13838

The problem is similar to rust-lang#13223: we've been skipping non-empty binders, letting lifetime bound variables escape.

I ended up refactoring `hir_ty::callable_sig_from_fnonce()`. Like rust-lang#13223, I chose to make use of `InferenceTable` which is capable of handling variables (I feel we should always use it when we solve trait-related stuff instead of manually building obligations/queries).

I couldn't make up a test that crashes without this patch (since the function I'm fixing is only used *outside* `hir-ty`, simple `hir-ty` test wouldn't cause crash), but at least I tested with my local build and made sure it doesn't crash with the code in the original issue. I'd appreciate any help to find a regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants