Use FmtPrinter instead of creating Instance

This commit is contained in:
clubby789 2022-09-23 12:40:20 +01:00
parent d1ec75da7c
commit 02025b54ea
4 changed files with 156 additions and 33 deletions

View File

@ -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;
@ -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).
@ -407,10 +408,11 @@ 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() {
.map(|arg| {
if !arg.is_suggestable(tcx, true) {
match arg.unpack() {
GenericArgKind::Lifetime(_) => self
.next_region_var(RegionVariableOrigin::MiscVariable(
rustc_span::DUMMY_SP,
@ -431,11 +433,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
)
.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()),
@ -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),
}
}

View File

@ -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<'_, '_> {

View File

@ -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
}

View File

@ -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
@ -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>`
@ -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`.