Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use FmtPrinter instead of creating Instance
  • Loading branch information
clubby789 committed Nov 5, 2022
commit 02025b54eade665d32ab9b507bb5a0376d639785
63 changes: 35 additions & 28 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
use rustc_middle::traits::util::supertraits;
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
use rustc_middle::ty::print::with_crate_prefix;
use rustc_middle::ty::{self, DefIdTree, GenericArgKind, ToPredicate, Ty, TyCtxt, TypeVisitable};
use rustc_middle::ty::{
self, DefIdTree, GenericArg, GenericArgKind, ToPredicate, Ty, TyCtxt, TypeVisitable,
};
use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Symbol;
Expand Down Expand Up @@ -283,7 +285,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
return None;
}

span = item_name.span;

// Don't show generic arguments when the method can't be found in any implementation (#81576).
Expand Down Expand Up @@ -407,35 +408,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(ty::Adt(def, _), ty::Adt(def_actual, substs)) if def == def_actual => {
// If there are any inferred arguments, (`{integer}`), we should replace
// them with underscores to allow the compiler to infer them
let substs = substs
let infer_substs: Vec<GenericArg<'_>> = substs
.into_iter()
.filter(|arg| !arg.is_suggestable(tcx, true))
.map(|arg| match arg.unpack() {
GenericArgKind::Lifetime(_) => self
.next_region_var(RegionVariableOrigin::MiscVariable(
rustc_span::DUMMY_SP,
))
.into(),
GenericArgKind::Type(_) => self
.next_ty_var(TypeVariableOrigin {
span: rustc_span::DUMMY_SP,
kind: TypeVariableOriginKind::MiscVariable,
})
.into(),
GenericArgKind::Const(arg) => self
.next_const_var(
arg.ty(),
ConstVariableOrigin {
.map(|arg| {
if !arg.is_suggestable(tcx, true) {
match arg.unpack() {
GenericArgKind::Lifetime(_) => self
.next_region_var(RegionVariableOrigin::MiscVariable(
rustc_span::DUMMY_SP,
))
.into(),
GenericArgKind::Type(_) => self
.next_ty_var(TypeVariableOrigin {
span: rustc_span::DUMMY_SP,
kind: ConstVariableOriginKind::MiscVariable,
},
)
.into(),
kind: TypeVariableOriginKind::MiscVariable,
})
.into(),
GenericArgKind::Const(arg) => self
.next_const_var(
arg.ty(),
ConstVariableOrigin {
span: rustc_span::DUMMY_SP,
kind: ConstVariableOriginKind::MiscVariable,
},
)
.into(),
}
} else {
arg
}
})
.collect::<Vec<_>>();
format!(
"{}",
ty::Instance::new(def_actual.did(), tcx.intern_substs(&substs))

tcx.value_path_str_with_substs(
def_actual.did(),
tcx.intern_substs(&infer_substs),
)
}
_ => self.ty_to_value_string(ty.peel_refs()),
Expand Down Expand Up @@ -1861,7 +1868,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// Print out the type for use in value namespace.
fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
match ty.kind() {
ty::Adt(def, substs) => format!("{}", ty::Instance::new(def.did(), substs)),
ty::Adt(def, substs) => self.tcx.def_path_str_with_substs(def.did(), substs),
_ => self.ty_to_string(ty),
}
}
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,12 @@ impl<'t> TyCtxt<'t> {
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
FmtPrinter::new(self, ns).print_def_path(def_id, substs).unwrap().into_buffer()
}

pub fn value_path_str_with_substs(self, def_id: DefId, substs: &'t [GenericArg<'t>]) -> String {
let ns = guess_def_namespace(self, def_id);
debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);
FmtPrinter::new(self, ns).print_value_path(def_id, substs).unwrap().into_buffer()
}
}

impl fmt::Write for FmtPrinter<'_, '_> {
Expand Down
17 changes: 16 additions & 1 deletion src/test/ui/suggestions/suggest-assoc-fn-call-with-turbofish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ struct GenericAssocMethod<T>(T);

impl<T> GenericAssocMethod<T> {
fn default_hello() {}
fn self_ty_hello(_: T) {}
fn self_ty_ref_hello(_: &T) {}
}

fn main() {
let x = GenericAssocMethod(33i32);
// Test for inferred types
let x = GenericAssocMethod(33);
x.default_hello();
//~^ ERROR no method named `default_hello` found
x.self_ty_ref_hello();
//~^ ERROR no method named `self_ty_ref_hello` found
x.self_ty_hello();
//~^ ERROR no method named `self_ty_hello` found
// Test for known types
let y = GenericAssocMethod(33i32);
y.default_hello();
//~^ ERROR no method named `default_hello` found
y.self_ty_ref_hello();
//~^ ERROR no method named `self_ty_ref_hello` found
y.self_ty_hello();
//~^ ERROR no method named `self_ty_hello` found
}
103 changes: 99 additions & 4 deletions src/test/ui/suggestions/suggest-assoc-fn-call-with-turbofish.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0599]: no method named `default_hello` found for struct `GenericAssocMethod<i32>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:9:7
error[E0599]: no method named `default_hello` found for struct `GenericAssocMethod<{integer}>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:12:7
|
LL | struct GenericAssocMethod<T>(T);
| ---------------------------- method `default_hello` not found for this struct
Expand All @@ -8,7 +8,7 @@ LL | x.default_hello();
| --^^^^^^^^^^^^^
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `GenericAssocMethod::default_hello`
| help: use associated function syntax instead: `GenericAssocMethod::<_>::default_hello`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
Expand All @@ -17,6 +17,101 @@ note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
LL | fn default_hello() {}
| ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
error[E0599]: no method named `self_ty_ref_hello` found for struct `GenericAssocMethod<{integer}>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:14:7
|
LL | struct GenericAssocMethod<T>(T);
| ---------------------------- method `self_ty_ref_hello` not found for this struct
...
LL | x.self_ty_ref_hello();
| --^^^^^^^^^^^^^^^^^
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `GenericAssocMethod::<_>::self_ty_ref_hello`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:6:5
|
LL | fn self_ty_ref_hello(_: &T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `self_ty_hello` found for struct `GenericAssocMethod<{integer}>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:16:7
|
LL | struct GenericAssocMethod<T>(T);
| ---------------------------- method `self_ty_hello` not found for this struct
...
LL | x.self_ty_hello();
| --^^^^^^^^^^^^^
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `GenericAssocMethod::<_>::self_ty_hello`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:5:5
|
LL | fn self_ty_hello(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `default_hello` found for struct `GenericAssocMethod<i32>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:20:7
|
LL | struct GenericAssocMethod<T>(T);
| ---------------------------- method `default_hello` not found for this struct
...
LL | y.default_hello();
| --^^^^^^^^^^^^^
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::default_hello`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:4:5
|
LL | fn default_hello() {}
| ^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `self_ty_ref_hello` found for struct `GenericAssocMethod<i32>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:22:7
|
LL | struct GenericAssocMethod<T>(T);
| ---------------------------- method `self_ty_ref_hello` not found for this struct
...
LL | y.self_ty_ref_hello();
| --^^^^^^^^^^^^^^^^^
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::self_ty_ref_hello`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:6:5
|
LL | fn self_ty_ref_hello(_: &T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `self_ty_hello` found for struct `GenericAssocMethod<i32>` in the current scope
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:24:7
|
LL | struct GenericAssocMethod<T>(T);
| ---------------------------- method `self_ty_hello` not found for this struct
...
LL | y.self_ty_hello();
| --^^^^^^^^^^^^^
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::self_ty_hello`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:5:5
|
LL | fn self_ty_hello(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0599`.