提交 900af2c6 编写于 作者: H Huon Wilson

lint: default methods must be called on Self to unconditionally recur.

This catches the case when a trait defines a default method that calls
itself, but on a type that isn't necessarily `Self`, e.g. there's no
reason that `T = Self` in the following, so the call isn't necessarily
recursive (`T` may override the call).

    trait Bar {
        fn method<T: Bar>(&self, x: &T) {
            x.method(x)
        }
    }

Fixes #26333.
上级 b1931e48
...@@ -2007,6 +2007,15 @@ fn id_refers_to_this_method<'tcx>(tcx: &ty::ctxt<'tcx>, ...@@ -2007,6 +2007,15 @@ fn id_refers_to_this_method<'tcx>(tcx: &ty::ctxt<'tcx>,
// method instead. // method instead.
ty::MethodTypeParam( ty::MethodTypeParam(
ty::MethodParam { ref trait_ref, method_num, impl_def_id: None, }) => { ty::MethodParam { ref trait_ref, method_num, impl_def_id: None, }) => {
let on_self = m.substs.self_ty().map_or(false, |t| t.is_self());
if !on_self {
// we can only be recurring in a default
// method if we're being called literally
// on the `Self` type.
return false
}
tcx.trait_item(trait_ref.def_id, method_num).def_id() tcx.trait_item(trait_ref.def_id, method_num).def_id()
} }
......
...@@ -67,4 +67,11 @@ fn all_fine() { ...@@ -67,4 +67,11 @@ fn all_fine() {
let _f = all_fine; let _f = all_fine;
} }
// issue 26333
trait Bar {
fn method<T: Bar>(&self, x: &T) {
x.method(x)
}
}
fn main() {} fn main() {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册