I’ve encountered an very weird issue that concerns the shadowing behavior of the match
expression, but I’m not sure if it’s intended or not. I’m using rustc 1.23.0-nightly (fa26421f5 2017-11-15)
.
First off, I’m using Diesel
and it is in that context that I’ve encountered this behavior, and thus the code sample reflects this. However I do not believe that crate to be the direct cause (i.e. this could happen in any crate, in principle).
As for the issue itself, consider this block of code:
{
let result: Result<Vec<QueriedMsg>, DieselError> = db.query_all();
match result {
Ok(messages) => {
let messages: Vec<QueriedMsg> = messages;
// More code
},
Err(diesel_err) => {
println!("Error querying the DB: {:#?}", diesel_err);
},
}
}
Consider the Ok(messages)
case. As-is, the code will compile fine.
However, originally messages
was msgs
, which causes the following compile error:
error[E0308]: mismatched types
--> src/bin/amplify/vis_server.rs:100:28
|
100 | Ok(msgs) => {
| ^^^^ expected struct `std::vec::Vec`, found struct `amplify::database::schema::msgs::table`
|
= note: expected type `std::vec::Vec<amplify::database::QueriedMsg>`
found type `amplify::database::schema::msgs::table`
error[E0308]: mismatched types
--> src/bin/amplify/vis_server.rs:101:57
|
101 | let msgs: Vec<QueriedMsg> = msgs;
| ^^^^ expected struct `std::vec::Vec`, found struct `amplify::database::schema::msgs::table`
|
= note: expected type `std::vec::Vec<amplify::database::QueriedMsg>`
found type `amplify::database::schema::msgs::table`
error[E0308]: mismatched types
--> src/bin/amplify/vis_server.rs:101:33
|
101 | let msgs: Vec<QueriedMsg> = msgs;
| ^^^^ expected struct `std::vec::Vec`, found struct `amplify::database::schema::msgs::table`
|
= note: expected type `std::vec::Vec<amplify::database::QueriedMsg>`
found type `amplify::database::schema::msgs::table`
error: aborting due to 3 previous errors
error: Could not compile `amplify-core`.
To learn more, run the command again with --verbose.
Cargo-Process exited abnormally with code 101 at Thu Nov 16 16:35:13
Here’s the odd part: while the named “type” exists, it’s in a module that I don’t explicitly use
anywhere in the module i.e. in amplify::database::schema::msgs
. I do have a glob import:
use amplify::database::schema::msgs::dsl::*;
However, even if that imports something that could potentially collide, I expect local name bindings in general to bluntly shadow the other identifier and thus not cause a collision. However, that seems to be exactly what’s happening there. Could anybody help clear this behavior up?