-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Rust tries to be too helpful with impl suggestions, sometimes being unhelpful #28894
Comments
That was supposed to work. |
I'm tired of seeing rustc recommend that I implement `Column` whenever I'm having issues resolving `SelectableExpression`. It's making things hard to debug, and I don't want to wait for rust-lang/rust#28894 to be fixed. The error behavior is awfully peculiar. I have no idea why this changed causes Rust to not even try and compile the second version of the same expression.
Anyone have any opinions on my recommendation on how to improve this, or other possibilities? |
Is there anything I can do to get some attention on this? We're at the point where we're considering some sweeping/painful changes to our trait hierarchy to work around this, as there are cases that have been consistent sources of confusion in Diesel. (Our particular problem case, among others, is that rustc notices that we have I can't imagine that we have the only case where recommending the blanket impl without mentioning the actual trait it's trying to satisfy is problematic. |
I think the obligation forest provides the functionality needed to fix this properly: #30976 Minified example: trait Expression {}
trait Column {}
impl<C: Column> Expression for C {}
fn assert_expression<T: Expression>() {}
fn main() {
assert_expression::<()>();
}
|
It seems like your example demonstrates the problem, not that it's fixed? |
Yes, it's just a minified version of your code above. Sorry for not being clear about that. |
Had another user get bit by this today: diesel-rs/diesel#323 |
This is fixed on 1.10/nightly:
|
I don't think this issue is fixed. This at least mentions the trait name, which is an improvement, but the error message is still confusing to me. In the example that you gave, it makes it sound like The case in diesel-rs/diesel#323 is also giving incorrect information.
|
It's still fairly trivial to get a case where the trait that's failing to apply isn't even mentioned. https://is.gd/DjS6Uk @arielb1 I think there's still some work to do on this one, can we reopen this issue? |
That looks like a different bug. Are investigating pub struct Text;
pub trait Expression {
type SqlType;
}
pub trait AsExpression<ST> {
type Expression: Expression<SqlType=ST>;
fn as_expression(self) -> Self::Expression;
}
impl<T, ST> AsExpression<ST> for T where
T: Expression<SqlType=ST>,
{
type Expression = Self;
fn as_expression(self) -> Self::Expression {
self
}
}
fn main() {
AsExpression::as_expression(Text);
} |
In your case, the reported-upon |
I would like to refactor that code before we make any changes to it. cc @eddyb. (https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/check/mod.rs#L4364) |
@arielb1 I will... try not to touch any of that. Except if I do anything to Do you want to track the reason for those obligations more accurately? |
The problem is that we add obligations for the trait whose def-id refers to the method. If we would just add the FnSpace obligations + the |
@arielb1 Ah, I agree then that a single predicate would be better for cause tracking (maybe even more efficient?). |
Do you have any plans for refactoring the above code, or can I try to hack on it myself? |
@arielb1 Go ahead, don't mind me. |
I have faced a similar problem when More details: https://users.rust-lang.org/t/unexpected-behaviors-of-trait-bounds/12286 |
Triage: other than the new format, the error message remains the same:
|
For nightly users, libraries can leverage |
|
@sgrif that's because you need to give the
|
This issue is about the fact that `Column` is irrelevant, not useful to
users when the missing trait is `AsExpression`, and there's no way to
control this behavior as a library author.
…On Tue, May 21, 2019 at 12:06 PM Esteban Kuber ***@***.***> wrote:
@sgrif <https://github.com/sgrif> that's because you need to give the
on_unimplemented to trait Column
<https://play.rust-lang.org/?version=nightly&mode=debug&edition=2015&gist=f12573f27165332f884dfd1abafc11a2>
:
#[rustc_on_unimplemented(
on(
_Self="&str",
message="found &str but we want a `Column`",
label="not on a `Column`",
note="you should be fruxolizing your thingamathings"
),
message="default message"
)]
trait Column {
type SqlType;
}
error[E0277]: found &str but we want a `Column`
--> src/main.rs:50:26
|
50 | let predicate = name.eq("Sean");
| ^^ not on a `Column`
|
= help: the trait `Column` is not implemented for `&str`
= note: you should be fruxolizing your thingamathings
= note: required because of the requirements on the impl of `Expression` for `&str`
= note: required because of the requirements on the impl of `AsExpression<std::string::String>` for `&str`
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#28894?email_source=notifications&email_token=AALVMKYC3WA66OPJP7SENATPWQ22DA5CNFSM4BRLSFGKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV4W4EA#issuecomment-494497296>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AALVMK5PICBHX6LQGOU3EDDPWQ22DANCNFSM4BRLSFGA>
.
--
Thanks,
Sean Griffin
|
Could you show us the "fixed" code for the example provided? I believe that I understand where you're coming from, as this is a pretty bad limitation |
https://gist.github.com/sgrif/fb06eec01a38a8f7a8f0ff9c1f62be7d
For what it's worth, #51992 is
probably enough to address this
…On Tue, May 21, 2019 at 12:55 PM Esteban Kuber ***@***.***> wrote:
Could you show us the "fixed" code for the example provided?
I believe that I understand where you're coming from, as this is a pretty
bad limitation rustc has, but I also think that the subset of problems
that are *expected* to happen (like this one) can receive specific error
messages that point people in the right direction, even though they need to
be added to the "wrong" trait. I'm not saying that this ticket should be
closed, as rustc should behave in a more reasonable manner, but rather
that diesel *can* improve its user friendliness *today* by judicious use
of on_unimplemented for common problems.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#28894?email_source=notifications&email_token=AALVMK5BGJHDGW3DRJJNLITPWRATHA5CNFSM4BRLSFGKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV43CVQ#issuecomment-494514518>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AALVMK5INSIXFPW75NWHR5LPWRATHANCNFSM4BRLSFGA>
.
--
Thanks,
Sean Griffin
|
Current output, minor change to the span:
|
Current output, more context for the requirements, but no other changes:
Crazy idea: what if crates started adding comments next to places where spans are displayed, like 👀
|
Current output:
|
Triage: no change. |
This code example is a simplified extraction of part of the hierarchy that I'm working on for https://github.com/sgrif/yaqb. It's still a bit contrived, but I think it really gets the point across about how unhelpful/nonsensical the resulting error message is.
The type structure goes like this:
AsExpression
represents a type which can be converted to anExpression
which would return a given type.Expression
represents... well, an expression.AsExpression
gets a blanket impl for any type which implementsExpression
.Column
represents a concrete column (which in the real code has other things associated to it). Ultimately the column has a type, and can be used as an expression of that type. Any type that implementsColumn
gets a blanket impl forExpression
.In
main
, we're trying to compare a column representing ani32
, to astr
. This is incorrect, and should fail to compile. However, the error message we get out of this is:In the context of a SQL database, and what
Column
means, this is a nonsense error, and would not be helpful to the user in seeing why their code is failing to compile. Rust is just being super clever and realizing that the blanket impls would apply if we just implement that instead. I'd like to propose changing the error message to something with a bit more information, such as:The text was updated successfully, but these errors were encountered: