-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Method resolution can be influenced in unexpected ways by external crates. #90907
Comments
It just occurred to me how it’s even possible to make use std::cell::RefCell;
fn foo(x: &RefCell<Box<[u8]>>) {
let y = x.borrow();
let z: &[u8] = y.as_ref();
println!("{:?}", z);
}
fn main() {
foo(&RefCell::new(Box::new([1, 2, 3_u8])));
} misbehave. (Note the explicit type signature See this branch for how it’s done. |
This doesn't look like method lookup to me (EDIT: okay, I guess autoderef would let It's long been known that the "only one remaining applicable What's worse here IMO is the design of the When you do A "correct" design would require writing something like (EDIT: something I forgot to include here is that I know this kind of thing is not that helpful but if I were to make a suggestion (for e.g. lints) is that (Something similar applies to Oh and currently, |
I guess the main thing that bugs me here is that when calling a standard library trait method ( I would probably prefer if it’d be made necessary to somehow mention the The other thing that bothers me is that method inference auto-derefs Both things listed above can have the unfortunate outcome that code changes behavior depending on what dependencies a crate has. If it merely would be possible that code stops compiling successfully due to an additional dependency with additional @eddyb Note that this issue is not really about I’m aware of possible inference failures that can be introduced by new trait implementations, this issue is mainly about behavior change i.e. successfully compiling code turns into successfully compiling code with different behavior. I do agree with the assertion that |
I guess I largely agree with you on most of those points. I do not have a lot of confidence in us being able to restrict behavior here, but it would be interesting to do a crater run where we compare the crates mentioned in It's definitely weird to have a crate "show up out of nowhere", but I don't have any good ideas for a predicate during trait solving (or even coherence), because of the "global" nature of trait solving - the best I can come up with is detecting the surprising situation after the fact and emitting a lint warning. |
Here's another case I believe related to this issue, or at least another case derived from the "only one remaining applicable impl" desig: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7c1a2e56a4ccb316bd7480494c44225c |
This code now returns what the user expects |
@Dylan-DPC I don't think anything changed here. Are you maybe missing the "malicious" code?
|
Ah nvm, my bad missed something in the setup :P |
I tried this code:
I expected to see this happen: Either the code should fail to compile or it should print
[1, 2, 3]
(the latter is what currently happens when the above is executed in the playground).Instead, this happened: The code prints
[21, 42, 63]
Context
What I didn’t tell you, my crate also contains this code
other than that, there are no other traits in scope, etc; no potential for any surprises, really there’s nothing else; you’ve seen the whole source code of a crate that prints
[21, 42, 63]
.More context
Now, the dependency in question offers only a single public thing, a function
Of course it also has some
but that clearly isn’t used in the
useful_api
implementation, so it’s probably some tests or whatever, entirely unrelated, and I don’t have to worry about, correct? Well apparently not correct.Here’s the whole source code of
some_dependency
(click to expand).Full reproduction guide:
git clone https://github.com/steffahn/iffy_methods.git cd iffy_methods cargo run
Is this a known issue? Is this something that needs to be fixed? @rustbot label T-lang, T-compiler, A-traits, A-security
The text was updated successfully, but these errors were encountered: