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

test(borrow): add a test to ensure borrow works #6

Closed
wants to merge 1 commit into from

Conversation

Ryman
Copy link
Contributor

@Ryman Ryman commented Nov 22, 2015

Ran into this when trying to remove some unsafety from hyper.

This works on stable but seems to be a regression on beta and nightly. Placing this here for easy reference/repro.

src/lib.rs:142:17: 142:68 error: cannot infer an appropriate lifetime due to conflicting requirements [E0495]
src/lib.rs:142             map.get::<UniCase<Cow<'key, str>>>(&UniCase(fn_borrow))
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:140:9: 143:10 help: consider using an explicit lifetime parameter as shown: fn get_shorter<'key>(map: &'key HashMap<UniCase<Cow<'static, str>>, u32>,
                     s: &'key str) -> Option<&'key u32>
src/lib.rs:140         fn get_shorter<'map, 'key>(map: &'map HashMap<UniCase<Cow<'static, str>>, u32>, s: &'key str) -> Option<&'map u32>{
src/lib.rs:141             let fn_borrow: Cow<'key, str> = s.into();
src/lib.rs:142             map.get::<UniCase<Cow<'key, str>>>(&UniCase(fn_borrow))
src/lib.rs:143         }
error: aborting due to previous error

Note that it works if you adjust the lifetimes to the suggestion, but that would limit the return value's lifetime to the lifetime of the key which would be undesirable for many cases.


fn get_shorter<'map, 'key>(map: &'map HashMap<UniCase<Cow<'static, str>>, u32>, s: &'key str) -> Option<&'map u32>{
let fn_borrow: Cow<'key, str> = s.into();
map.get::<UniCase<Cow<'key, str>>>(&UniCase(fn_borrow))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This get works with and without the type-hint on stable (1.4), fails in both cases on beta and nightly.

@seanmonstar
Copy link
Owner

Is this testing something specific about UniCase, or just lifetimes and Cow?

@Ryman
Copy link
Contributor Author

Ryman commented Nov 23, 2015

The main intention was to test that Borrow actually worked through unicase, ensuring it works with HashMaps, but you're right in that it's not a very specific test. The verdict on the rust issue seems to be that it's a soundness issue and it's unlikely to compile as-is in the future.

@Ryman
Copy link
Contributor Author

Ryman commented Jan 2, 2016

Closing for now, don't think there's a good way to do this currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants