You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::collections::HashMap;fnmain(){letmut map = HashMap::new();
map.insert("foo".to_string(),"bar".to_string());// Oh hey I can use borrow to do lookup with a string literal!println!("{}", map["foo"]);}// <anon>:5:20: 5:30 error: the trait `collections::borrow::Borrow<&str>` // is not implemented for the type `collections::string::String` [E0277]// <anon>:5 println!("{}", map["foo"]);// ^~~~~~~~~~
:(
use std::collections::HashMap;fnmain(){letmut map = HashMap::new();
map.insert("foo".to_string(),"bar".to_string());// Ok, maybe I can deref to cancel out the autoref?println!("{}", map[*"foo"]);}// error: internal compiler error: trying to take the // sizing type of str, an unsized type
:((((((
These two conveniences are basically cancelling each-other out for the super-natural case of doing lookup with a string literal. Is there something that can be done about this?
The ICE is definitely a bug, which I may have introduced: *"foo" is treated as a constant expression, but constant expressions have no notion of lvalues, so &*expr works for constant expressions iff *expr is sized (and can be treated as an rvalue).
The following works, btw:
use std::collections::HashMap;fnmain(){letmut map = HashMap::new();
map.insert("foo".to_string(),"bar".to_string());// Ok, maybe I can deref to cancel out the autoref?let key = "foo";println!("{}", map[*key]);}
Nominating: we need to at least get the * version working before beta. It would be nice if the more natural version worked as well, but that may require more substantial changes to indexing.
its not clear whether we have time for a breaking-change to try to fix the trait.
(and its also not clear whether a breaking-change is the only option for fixing the trait; e.g. there may be some method via deref-coercion or other magic.)
Activity
Gankra commentedon Feb 26, 2015
CC @aturon
eddyb commentedon Feb 26, 2015
The ICE is definitely a bug, which I may have introduced:
*"foo"is treated as a constant expression, but constant expressions have no notion of lvalues, so&*exprworks for constant expressions iff*expris sized (and can be treated as an rvalue).The following works, btw:
Gankra commentedon Feb 26, 2015
Yeah, that was my work-around, but I think we can all agree that that's pretty awful.
aturon commentedon Mar 17, 2015
triage: I-nominated
aturon commentedon Mar 17, 2015
(Hm, triage bot seems to be busted.)
aturon commentedon Mar 17, 2015
Nominating: we need to at least get the
*version working before beta. It would be nice if the more natural version worked as well, but that may require more substantial changes to indexing.pnkfelix commentedon Mar 19, 2015
1.0 beta for addressing the ICE.
its not clear whether we have time for a breaking-change to try to fix the trait.
(and its also not clear whether a breaking-change is the only option for fixing the trait; e.g. there may be some method via deref-coercion or other magic.)
aturon commentedon Mar 20, 2015
Update: @nikomatsakis is looking into changing the traits to work by-value, as the RFC originally intended.
alexcrichton commentedon Mar 26, 2015
Now that index operators take arguments by value I believe this bug is actually fixed. Yay!
aturon commentedon Mar 26, 2015
Does the ICE have a separate issue?
alexcrichton commentedon Mar 26, 2015
If you do
&*"a"it looks like you run into #22894. If you do*"a"you get a nice legit error.