Skip to content

Confusing error message when using CellRef #41865

@spirali

Description

@spirali

Hello,

It took me some time to understand the following problem.

The following code ends with error:

error: the type of this value must be known in this context
  --> main.rs:14:34
   |
14 |     println!("Hello, world! {}", sb.flag);
   |                                  ^^^^^^^
use std::rc::Rc;
use std::cell::RefCell;
use std::borrow::Borrow;


pub struct S {
	flag : bool
}

type SCell = Rc<RefCell<S>>;

fn test(s : SCell) {
    let sb =  &s.borrow();
    println!("Hello, world! {}", sb.flag);
}

fn main() {
    let s = Rc::new(RefCell::new(S{flag: false}));
    test(s);
}

However, the similar construction works in many other situation and a natural fix (let sb : &S = &s.borrow();) does not work. I was not able to understand the problem until @gavento pointed out that the problem is with use std::borrow::Borrow;. When the line is removed, the code is compiled without any error.

I suggest to improve the error message in a way that indicates that problem is with the borrow method, since the current error message pointing to the structure usage is quite confusing (at least for me:).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions