-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add deref suggestion #43870
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
Add deref suggestion #43870
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
macro_rules! borrow { | ||
($x:expr) => { &$x } | ||
} | ||
|
||
fn foo(_: String) {} | ||
|
||
fn foo2(s: &String) { | ||
foo(s); | ||
} | ||
|
||
fn foo3(_: u32) {} | ||
fn foo4(u: &u32) { | ||
foo3(u); | ||
} | ||
|
||
fn main() { | ||
let s = String::new(); | ||
let r_s = &s; | ||
foo2(r_s); | ||
foo(&"aaa".to_owned()); | ||
foo(&mut "aaa".to_owned()); | ||
foo3(borrow!(0)); | ||
foo4(&0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw some logic around macros -- can we embed this in a macro and see what happens? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/deref-suggestion.rs:18:9 | ||
| | ||
18 | foo(s); | ||
| ^ expected struct `std::string::String`, found reference | ||
| | ||
= note: expected type `std::string::String` | ||
found type `&std::string::String` | ||
= help: here are some functions which might fulfill your needs: | ||
- .escape_debug() | ||
- .escape_default() | ||
- .escape_unicode() | ||
- .to_lowercase() | ||
- .to_uppercase() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated, but we really have to do something here. These suggestions are pretty silly. |
||
|
||
error[E0308]: mismatched types | ||
--> $DIR/deref-suggestion.rs:23:10 | ||
| | ||
23 | foo3(u); | ||
| ^ expected u32, found &u32 | ||
| | ||
= note: expected type `u32` | ||
found type `&u32` | ||
= help: try with `*u` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/deref-suggestion.rs:30:9 | ||
| | ||
30 | foo(&"aaa".to_owned()); | ||
| ^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found reference | ||
| | ||
= note: expected type `std::string::String` | ||
found type `&std::string::String` | ||
= help: try with `"aaa".to_owned()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/deref-suggestion.rs:31:9 | ||
| | ||
31 | foo(&mut "aaa".to_owned()); | ||
| ^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found mutable reference | ||
| | ||
= note: expected type `std::string::String` | ||
found type `&mut std::string::String` | ||
= help: try with `"aaa".to_owned()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/deref-suggestion.rs:12:20 | ||
| | ||
12 | ($x:expr) => { &$x } | ||
| ^^^ expected u32, found &{integer} | ||
... | ||
32 | foo3(borrow!(0)); | ||
| ---------- in this macro invocation | ||
| | ||
= note: expected type `u32` | ||
found type `&{integer}` | ||
|
||
error: aborting due to 5 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess @GuillaumeGomez that this particular macro is triggering this case. Try having the macro produce
{&x}
for example -- although this is also an interesting data point.