Skip to content

Commit 02025b5

Browse files
committed
Use FmtPrinter instead of creating Instance
1 parent d1ec75d commit 02025b5

File tree

4 files changed

+156
-33
lines changed

4 files changed

+156
-33
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
2222
use rustc_middle::traits::util::supertraits;
2323
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
2424
use rustc_middle::ty::print::with_crate_prefix;
25-
use rustc_middle::ty::{self, DefIdTree, GenericArgKind, ToPredicate, Ty, TyCtxt, TypeVisitable};
25+
use rustc_middle::ty::{
26+
self, DefIdTree, GenericArg, GenericArgKind, ToPredicate, Ty, TyCtxt, TypeVisitable,
27+
};
2628
use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef};
2729
use rustc_span::symbol::{kw, sym, Ident};
2830
use rustc_span::Symbol;
@@ -283,7 +285,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
283285
) {
284286
return None;
285287
}
286-
287288
span = item_name.span;
288289

289290
// Don't show generic arguments when the method can't be found in any implementation (#81576).
@@ -407,35 +408,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
407408
(ty::Adt(def, _), ty::Adt(def_actual, substs)) if def == def_actual => {
408409
// If there are any inferred arguments, (`{integer}`), we should replace
409410
// them with underscores to allow the compiler to infer them
410-
let substs = substs
411+
let infer_substs: Vec<GenericArg<'_>> = substs
411412
.into_iter()
412-
.filter(|arg| !arg.is_suggestable(tcx, true))
413-
.map(|arg| match arg.unpack() {
414-
GenericArgKind::Lifetime(_) => self
415-
.next_region_var(RegionVariableOrigin::MiscVariable(
416-
rustc_span::DUMMY_SP,
417-
))
418-
.into(),
419-
GenericArgKind::Type(_) => self
420-
.next_ty_var(TypeVariableOrigin {
421-
span: rustc_span::DUMMY_SP,
422-
kind: TypeVariableOriginKind::MiscVariable,
423-
})
424-
.into(),
425-
GenericArgKind::Const(arg) => self
426-
.next_const_var(
427-
arg.ty(),
428-
ConstVariableOrigin {
413+
.map(|arg| {
414+
if !arg.is_suggestable(tcx, true) {
415+
match arg.unpack() {
416+
GenericArgKind::Lifetime(_) => self
417+
.next_region_var(RegionVariableOrigin::MiscVariable(
418+
rustc_span::DUMMY_SP,
419+
))
420+
.into(),
421+
GenericArgKind::Type(_) => self
422+
.next_ty_var(TypeVariableOrigin {
429423
span: rustc_span::DUMMY_SP,
430-
kind: ConstVariableOriginKind::MiscVariable,
431-
},
432-
)
433-
.into(),
424+
kind: TypeVariableOriginKind::MiscVariable,
425+
})
426+
.into(),
427+
GenericArgKind::Const(arg) => self
428+
.next_const_var(
429+
arg.ty(),
430+
ConstVariableOrigin {
431+
span: rustc_span::DUMMY_SP,
432+
kind: ConstVariableOriginKind::MiscVariable,
433+
},
434+
)
435+
.into(),
436+
}
437+
} else {
438+
arg
439+
}
434440
})
435441
.collect::<Vec<_>>();
436-
format!(
437-
"{}",
438-
ty::Instance::new(def_actual.did(), tcx.intern_substs(&substs))
442+
443+
tcx.value_path_str_with_substs(
444+
def_actual.did(),
445+
tcx.intern_substs(&infer_substs),
439446
)
440447
}
441448
_ => self.ty_to_value_string(ty.peel_refs()),
@@ -1861,7 +1868,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18611868
/// Print out the type for use in value namespace.
18621869
fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
18631870
match ty.kind() {
1864-
ty::Adt(def, substs) => format!("{}", ty::Instance::new(def.did(), substs)),
1871+
ty::Adt(def, substs) => self.tcx.def_path_str_with_substs(def.did(), substs),
18651872
_ => self.ty_to_string(ty),
18661873
}
18671874
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,12 @@ impl<'t> TyCtxt<'t> {
16591659
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
16601660
FmtPrinter::new(self, ns).print_def_path(def_id, substs).unwrap().into_buffer()
16611661
}
1662+
1663+
pub fn value_path_str_with_substs(self, def_id: DefId, substs: &'t [GenericArg<'t>]) -> String {
1664+
let ns = guess_def_namespace(self, def_id);
1665+
debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);
1666+
FmtPrinter::new(self, ns).print_value_path(def_id, substs).unwrap().into_buffer()
1667+
}
16621668
}
16631669

16641670
impl fmt::Write for FmtPrinter<'_, '_> {

src/test/ui/suggestions/suggest-assoc-fn-call-with-turbofish.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@ struct GenericAssocMethod<T>(T);
22

33
impl<T> GenericAssocMethod<T> {
44
fn default_hello() {}
5+
fn self_ty_hello(_: T) {}
6+
fn self_ty_ref_hello(_: &T) {}
57
}
68

79
fn main() {
8-
let x = GenericAssocMethod(33i32);
10+
// Test for inferred types
11+
let x = GenericAssocMethod(33);
912
x.default_hello();
1013
//~^ ERROR no method named `default_hello` found
14+
x.self_ty_ref_hello();
15+
//~^ ERROR no method named `self_ty_ref_hello` found
16+
x.self_ty_hello();
17+
//~^ ERROR no method named `self_ty_hello` found
18+
// Test for known types
19+
let y = GenericAssocMethod(33i32);
20+
y.default_hello();
21+
//~^ ERROR no method named `default_hello` found
22+
y.self_ty_ref_hello();
23+
//~^ ERROR no method named `self_ty_ref_hello` found
24+
y.self_ty_hello();
25+
//~^ ERROR no method named `self_ty_hello` found
1126
}
Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error[E0599]: no method named `default_hello` found for struct `GenericAssocMethod<i32>` in the current scope
2-
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:9:7
1+
error[E0599]: no method named `default_hello` found for struct `GenericAssocMethod<{integer}>` in the current scope
2+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:12:7
33
|
44
LL | struct GenericAssocMethod<T>(T);
55
| ---------------------------- method `default_hello` not found for this struct
@@ -8,7 +8,7 @@ LL | x.default_hello();
88
| --^^^^^^^^^^^^^
99
| | |
1010
| | this is an associated function, not a method
11-
| help: use associated function syntax instead: `GenericAssocMethod::default_hello`
11+
| help: use associated function syntax instead: `GenericAssocMethod::<_>::default_hello`
1212
|
1313
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
1414
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
@@ -17,6 +17,101 @@ note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
1717
LL | fn default_hello() {}
1818
| ^^^^^^^^^^^^^^^^^^
1919

20-
error: aborting due to previous error
20+
error[E0599]: no method named `self_ty_ref_hello` found for struct `GenericAssocMethod<{integer}>` in the current scope
21+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:14:7
22+
|
23+
LL | struct GenericAssocMethod<T>(T);
24+
| ---------------------------- method `self_ty_ref_hello` not found for this struct
25+
...
26+
LL | x.self_ty_ref_hello();
27+
| --^^^^^^^^^^^^^^^^^
28+
| | |
29+
| | this is an associated function, not a method
30+
| help: use associated function syntax instead: `GenericAssocMethod::<_>::self_ty_ref_hello`
31+
|
32+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
33+
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
34+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:6:5
35+
|
36+
LL | fn self_ty_ref_hello(_: &T) {}
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error[E0599]: no method named `self_ty_hello` found for struct `GenericAssocMethod<{integer}>` in the current scope
40+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:16:7
41+
|
42+
LL | struct GenericAssocMethod<T>(T);
43+
| ---------------------------- method `self_ty_hello` not found for this struct
44+
...
45+
LL | x.self_ty_hello();
46+
| --^^^^^^^^^^^^^
47+
| | |
48+
| | this is an associated function, not a method
49+
| help: use associated function syntax instead: `GenericAssocMethod::<_>::self_ty_hello`
50+
|
51+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
52+
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
53+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:5:5
54+
|
55+
LL | fn self_ty_hello(_: T) {}
56+
| ^^^^^^^^^^^^^^^^^^^^^^
57+
58+
error[E0599]: no method named `default_hello` found for struct `GenericAssocMethod<i32>` in the current scope
59+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:20:7
60+
|
61+
LL | struct GenericAssocMethod<T>(T);
62+
| ---------------------------- method `default_hello` not found for this struct
63+
...
64+
LL | y.default_hello();
65+
| --^^^^^^^^^^^^^
66+
| | |
67+
| | this is an associated function, not a method
68+
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::default_hello`
69+
|
70+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
71+
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
72+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:4:5
73+
|
74+
LL | fn default_hello() {}
75+
| ^^^^^^^^^^^^^^^^^^
76+
77+
error[E0599]: no method named `self_ty_ref_hello` found for struct `GenericAssocMethod<i32>` in the current scope
78+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:22:7
79+
|
80+
LL | struct GenericAssocMethod<T>(T);
81+
| ---------------------------- method `self_ty_ref_hello` not found for this struct
82+
...
83+
LL | y.self_ty_ref_hello();
84+
| --^^^^^^^^^^^^^^^^^
85+
| | |
86+
| | this is an associated function, not a method
87+
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::self_ty_ref_hello`
88+
|
89+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
90+
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
91+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:6:5
92+
|
93+
LL | fn self_ty_ref_hello(_: &T) {}
94+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
95+
96+
error[E0599]: no method named `self_ty_hello` found for struct `GenericAssocMethod<i32>` in the current scope
97+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:24:7
98+
|
99+
LL | struct GenericAssocMethod<T>(T);
100+
| ---------------------------- method `self_ty_hello` not found for this struct
101+
...
102+
LL | y.self_ty_hello();
103+
| --^^^^^^^^^^^^^
104+
| | |
105+
| | this is an associated function, not a method
106+
| help: use associated function syntax instead: `GenericAssocMethod::<i32>::self_ty_hello`
107+
|
108+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
109+
note: the candidate is defined in an impl for the type `GenericAssocMethod<T>`
110+
--> $DIR/suggest-assoc-fn-call-with-turbofish.rs:5:5
111+
|
112+
LL | fn self_ty_hello(_: T) {}
113+
| ^^^^^^^^^^^^^^^^^^^^^^
114+
115+
error: aborting due to 6 previous errors
21116

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

0 commit comments

Comments
 (0)