mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-28 01:34:21 +00:00
f13f37fd7b
Rework ptr-to-ref conversion suggestion for method calls If we have a value `z` of type `*const u8` and try to call `z.to_string()`, the upstream compiler will show you a note suggesting to call `<*const u8>::as_ref` first. This PR extends that: - The note will only be shown when the method would exist on the corresponding reference type - It can now suggest any of `<*const u8>::as_ref`, `<*mut u8>::as_ref` and `<*mut u8>::as_mut`, depending on what the method needs. I didn't introduce a `help` message because that's not a good idea with `unsafe` functions (and you'd also need to unwrap the `Option<&_>` somehow). People should check the safety requirements. For the simplest case ```rust fn main() { let x = 8u8; let z: *const u8 = &x; // issue #21596 println!("{}", z.to_string()); //~ ERROR E0599 } ``` the output changes like this: ```diff error[E0599]: `*const u8` doesn't implement `std::fmt::Display` --> $DIR/suggest-convert-ptr-to-ref.rs:5:22 | LL | println!("{}", z.to_string()); | ^^^^^^^^^ `*const u8` cannot be formatted with the default formatter | - = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref - = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior +note: the method `to_string` exists on the type `&u8` + --> $SRC_DIR/alloc/src/string.rs:LL:COL + = note: try using the unsafe method `<*const T>::as_ref` to get an optional reference to the value behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref = note: the following trait bounds were not satisfied: `*const u8: std::fmt::Display` which is required by `*const u8: ToString` ``` I removed the separate note about the safety requirements because it was incomplete and the linked doc page already has the information you need. Fixes #83695, but that's more of a side effect. The upstream compiler already suggests the right method name here. |
||
---|---|---|
.. | ||
auxiliary | ||
issues | ||
assign-to-method.rs | ||
assign-to-method.stderr | ||
call_method_unknown_pointee.rs | ||
call_method_unknown_pointee.stderr | ||
disambiguate-associated-function-first-arg.rs | ||
disambiguate-associated-function-first-arg.stderr | ||
disambiguate-multiple-blanket-impl.rs | ||
disambiguate-multiple-blanket-impl.stderr | ||
disambiguate-multiple-impl.rs | ||
disambiguate-multiple-impl.stderr | ||
disambiguate-multiple-trait-2.rs | ||
disambiguate-multiple-trait-2.stderr | ||
disambiguate-multiple-trait.rs | ||
disambiguate-multiple-trait.stderr | ||
field-method-suggestion-using-return-ty.rs | ||
field-method-suggestion-using-return-ty.stderr | ||
inherent-bound-in-probe.rs | ||
inherent-bound-in-probe.stderr | ||
issue-3707.rs | ||
issue-3707.stderr | ||
issue-7950.rs | ||
issue-7950.stderr | ||
issue-19521.rs | ||
issue-19521.stderr | ||
method-ambig-one-trait-unknown-int-type.rs | ||
method-ambig-one-trait-unknown-int-type.stderr | ||
method-ambig-two-traits-cross-crate.rs | ||
method-ambig-two-traits-cross-crate.stderr | ||
method-ambig-two-traits-from-bounds.rs | ||
method-ambig-two-traits-from-bounds.stderr | ||
method-ambig-two-traits-from-impls2.rs | ||
method-ambig-two-traits-from-impls2.stderr | ||
method-ambig-two-traits-from-impls.rs | ||
method-ambig-two-traits-from-impls.stderr | ||
method-ambig-two-traits-with-default-method.rs | ||
method-ambig-two-traits-with-default-method.stderr | ||
method-ambiguity-no-rcvr.rs | ||
method-ambiguity-no-rcvr.stderr | ||
method-argument-inference-associated-type.rs | ||
method-call-err-msg.rs | ||
method-call-err-msg.stderr | ||
method-call-lifetime-args-fail.rs | ||
method-call-lifetime-args-fail.stderr | ||
method-call-lifetime-args-lint-fail.rs | ||
method-call-lifetime-args-lint-fail.stderr | ||
method-call-lifetime-args-lint.rs | ||
method-call-lifetime-args-lint.stderr | ||
method-call-lifetime-args-subst-index.rs | ||
method-call-lifetime-args-unresolved.rs | ||
method-call-lifetime-args-unresolved.stderr | ||
method-call-lifetime-args.rs | ||
method-call-lifetime-args.stderr | ||
method-call-type-binding.rs | ||
method-call-type-binding.stderr | ||
method-deref-to-same-trait-object-with-separate-params.rs | ||
method-deref-to-same-trait-object-with-separate-params.stderr | ||
method-early-bound-lifetimes-on-self.rs | ||
method-lookup-order.rs | ||
method-macro-backtrace.rs | ||
method-macro-backtrace.stderr | ||
method-missing-call.rs | ||
method-missing-call.stderr | ||
method-mut-self-modifies-mut-slice-lvalue.rs | ||
method-normalize-bounds-issue-20604.rs | ||
method-not-found-but-doc-alias.rs | ||
method-not-found-but-doc-alias.stderr | ||
method-not-found-generic-arg-elision.rs | ||
method-not-found-generic-arg-elision.stderr | ||
method-on-ambiguous-numeric-type.rs | ||
method-on-ambiguous-numeric-type.stderr | ||
method-path-in-pattern.rs | ||
method-path-in-pattern.stderr | ||
method-probe-no-guessing-dyn-trait.rs | ||
method-projection.rs | ||
method-recursive-blanket-impl.rs | ||
method-recursive-blanket-impl.stderr | ||
method-resolvable-path-in-pattern.rs | ||
method-resolvable-path-in-pattern.stderr | ||
method-self-arg-1.rs | ||
method-self-arg-1.stderr | ||
method-self-arg-2.rs | ||
method-self-arg-2.stderr | ||
method-self-arg-aux1.rs | ||
method-self-arg-aux2.rs | ||
method-self-arg-trait.rs | ||
method-self-arg.rs | ||
method-trait-object-with-hrtb.rs | ||
method-two-trait-defer-resolution-1.rs | ||
method-two-trait-defer-resolution-2.rs | ||
method-two-trait-defer-resolution-2.stderr | ||
method-two-traits-distinguished-via-where-clause.rs | ||
method-two-traits-distinguished-via-where-clause.stderr | ||
method-where-clause.rs | ||
opaque_param_in_ufc.rs | ||
opaque_param_in_ufc.stderr | ||
probe-overflow-due-to-sized-predicate-ordering.rs | ||
suggest-convert-ptr-to-ref.rs | ||
suggest-convert-ptr-to-ref.stderr | ||
suggest-method-on-call-with-macro-rcvr.rs | ||
suggest-method-on-call-with-macro-rcvr.stderr |