Skip to content

Indexing Autoref + Borrow = :( #22826

Closed
Closed

Description

@Gankra
use std::collections::HashMap;
fn main(){
    let mut 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;
fn main(){
    let mut 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?

Activity

  1. Gankra commented on Feb 26, 2015

    @Gankra
    ContributorAuthor
  2. eddyb commented on Feb 26, 2015

    @eddyb
    Contributor

    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;
    fn main(){
        let mut 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]);
    }
  3. Gankra commented on Feb 26, 2015

    @Gankra
    ContributorAuthor

    Yeah, that was my work-around, but I think we can all agree that that's pretty awful.

  4. added
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    on Feb 26, 2015
  5. aturon commented on Mar 17, 2015

    @aturon
    Contributor

    triage: I-nominated

  6. aturon commented on Mar 17, 2015

    @aturon
    Contributor

    (Hm, triage bot seems to be busted.)

  7. aturon commented on Mar 17, 2015

    @aturon
    Contributor

    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.

  8. pnkfelix commented on Mar 19, 2015

    @pnkfelix
    Contributor

    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.)

  9. added this to the 1.0 beta milestone on Mar 19, 2015
  10. aturon commented on Mar 20, 2015

    @aturon
    Contributor

    Update: @nikomatsakis is looking into changing the traits to work by-value, as the RFC originally intended.

  11. alexcrichton commented on Mar 26, 2015

    @alexcrichton
    Member

    Now that index operators take arguments by value I believe this bug is actually fixed. Yay!

    use std::collections::HashMap;
    
    fn main() {
        let mut m = HashMap::new();
        m.insert("a".to_string(), 2);
        println!("{}", m["a"]);
    }
  12. aturon commented on Mar 26, 2015

    @aturon
    Contributor

    Does the ICE have a separate issue?

  13. alexcrichton commented on Mar 26, 2015

    @alexcrichton
    Member

    If you do &*"a" it looks like you run into #22894. If you do *"a" you get a nice legit error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priority

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions