|
| 1 | +// Show the diagnostic for cases where a method is expected by the user but a different one is |
| 2 | +// shaddowing it causing a field access to fail. Test for #93726. |
| 3 | + |
| 4 | +use std::cell::RefCell; |
| 5 | +use std::rc::Rc; |
| 6 | +use std::borrow::BorrowMut; |
| 7 | + |
| 8 | +type NodeAlias = Option<Rc<RefCell<Node>>>; |
| 9 | + |
| 10 | +#[derive(Clone, Debug, PartialEq)] |
| 11 | +struct Node { |
| 12 | + value: String, |
| 13 | + next: NodeAlias, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Clone, Debug, PartialEq)] |
| 17 | +struct TransactionLog { |
| 18 | + head: NodeAlias, |
| 19 | + tail: NodeAlias, |
| 20 | + length: i32, |
| 21 | +} |
| 22 | + |
| 23 | +impl Node { |
| 24 | + fn new(value: String) -> Rc<RefCell<Node>> { |
| 25 | + Rc::new(RefCell::new(Node { value, next: None })) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl TransactionLog { |
| 30 | + pub fn empty_list() -> TransactionLog { |
| 31 | + TransactionLog { |
| 32 | + head: None, |
| 33 | + tail: None, |
| 34 | + length: 0, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn append(&mut self, value: String) { |
| 39 | + let node = Node::new(value); |
| 40 | + match self.tail.take() { |
| 41 | + Some(old_tail) => old_tail.borrow_mut().next = Some(node.clone()), |
| 42 | + //~^ ERROR: no field `next` on type `&mut Rc<RefCell<Node>>` |
| 43 | + //~| NOTE: unknown field |
| 44 | + //~| NOTE: this method is `std::borrow::BorrowMut::borrow_mut` |
| 45 | + //~| NOTE: you might have meant to call `std::cell::RefCell::<T>::borrow_mut` instead |
| 46 | + //~| NOTE: this returns `&mut Rc<RefCell<Node>>` |
| 47 | + None => self.head = Some(node.clone()), |
| 48 | + } |
| 49 | + self.length += 1; |
| 50 | + self.tail = Some(node); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +struct S; |
| 56 | +struct Other { |
| 57 | + field: (), |
| 58 | +} |
| 59 | +impl Other { |
| 60 | + fn method(&self) {} |
| 61 | +} |
| 62 | + |
| 63 | +impl S { |
| 64 | + fn foo(&self) -> Other { Other { field: () } } |
| 65 | + //~^ NOTE: you might have meant to call |
| 66 | + //~| NOTE: you might have meant to call |
| 67 | + //~| NOTE: you might have meant to call |
| 68 | + //~| NOTE: you might have meant to call |
| 69 | +} |
| 70 | +mod a { |
| 71 | + use crate::{S, Other}; |
| 72 | + pub trait T { |
| 73 | + fn foo(&self) {} |
| 74 | + //~^ NOTE: this returns `()` |
| 75 | + //~| NOTE: this returns `()` |
| 76 | + //~| NOTE: this returns `()` |
| 77 | + //~| NOTE: this returns `()` |
| 78 | + } |
| 79 | + impl T for S {} |
| 80 | + impl T for Box<S> {} |
| 81 | + pub trait K<P> { |
| 82 | + fn foo(&self) -> Other { Other { field : () } } |
| 83 | + //~^ NOTE: you might have meant to call |
| 84 | + //~| NOTE: you might have meant to call |
| 85 | + //~| NOTE: you might have meant to call |
| 86 | + //~| NOTE: you might have meant to call |
| 87 | + } |
| 88 | + impl K<()> for S {} |
| 89 | + impl K<i32> for Box<S> {} |
| 90 | + pub trait V { |
| 91 | + fn foo(&self) -> Other; |
| 92 | + //~^ NOTE: you might have meant to call |
| 93 | + //~| NOTE: you might have meant to call |
| 94 | + //~| NOTE: you might have meant to call |
| 95 | + //~| NOTE: you might have meant to call |
| 96 | + } |
| 97 | + impl V for Box<S> { |
| 98 | + fn foo(&self) -> Other { Other { field : () } } |
| 99 | + } |
| 100 | + pub trait U { |
| 101 | + fn foo(&self) {} // not suggested because its return type doesn't have the field |
| 102 | + } |
| 103 | + impl U for Box<S> {} |
| 104 | +} |
| 105 | +use a::T; |
| 106 | + |
| 107 | +fn main() { |
| 108 | + let x = Box::new(S); |
| 109 | + x.foo().field; //~ ERROR: no field `field` on type `()` |
| 110 | + //~^ NOTE: unknown field |
| 111 | + //~| NOTE: this method is `a::T::foo` |
| 112 | + let y = x.foo(); |
| 113 | + //~^ NOTE: this method is `a::T::foo` |
| 114 | + //~| NOTE: this method is `a::T::foo` |
| 115 | + let _ = y.field; //~ ERROR: no field `field` on type `()` |
| 116 | + //~^ NOTE: unknown field |
| 117 | + x.foo().method(); //~ ERROR: no method named `method` found for unit type `()` in the current scope |
| 118 | + //~^ NOTE: method not found in `()` |
| 119 | + //~| NOTE: this method is `a::T::foo` |
| 120 | + let _ = y.method(); //~ ERROR: no method named `method` found for unit type `()` in the current scope |
| 121 | + //~^ NOTE: method not found in `()` |
| 122 | +} |
0 commit comments