Skip to content

Commit d342cee

Browse files
authored
Merge pull request rust-lang#5434 from eddyb/rustup
rustup: update for the new Ty::walk interface. The first commit fixes a portability bug in `setup-toolchain.sh`, while the second rewrites the handling of "trait impl methods" in `use_self` - even if `Ty::walk` could've still been used, it was IMO a misuse. This could also serve as a PSA: *please* use `hir_ty_to_ty` instead of trying to compare `hir::Ty`s between themselves or against semantic `Ty`s. Its "quasi-deprecation" is 3 years old and doesn't really mean anything, just that it's currently uncached and that we should eventually querify it (either for a single HIR node, or for all of the nodes in an entire definition). --- changelog: none
2 parents 7907abe + f5b6a0c commit d342cee

File tree

4 files changed

+68
-62
lines changed

4 files changed

+68
-62
lines changed

clippy_lints/src/let_underscore.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use if_chain::if_chain;
22
use rustc_hir::{Local, PatKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_middle::lint::in_external_macro;
5+
use rustc_middle::ty::subst::GenericArgKind;
56
use rustc_session::{declare_lint_pass, declare_tool_lint};
67

78
use crate::utils::{is_must_use_func_call, is_must_use_ty, match_type, paths, span_lint_and_help};
@@ -75,8 +76,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
7576
if let PatKind::Wild = local.pat.kind;
7677
if let Some(ref init) = local.init;
7778
then {
78-
let check_ty = |ty| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, ty, path));
79-
if cx.tables.expr_ty(init).walk().any(check_ty) {
79+
let init_ty = cx.tables.expr_ty(init);
80+
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
81+
GenericArgKind::Type(inner_ty) => {
82+
SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
83+
},
84+
85+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
86+
});
87+
if contains_sync_guard {
8088
span_lint_and_help(
8189
cx,
8290
LET_UNDERSCORE_LOCK,

clippy_lints/src/methods/mod.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hir::intravisit::{self, Visitor};
1515
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
1616
use rustc_middle::hir::map::Map;
1717
use rustc_middle::lint::in_external_macro;
18+
use rustc_middle::ty::subst::GenericArgKind;
1819
use rustc_middle::ty::{self, Predicate, Ty};
1920
use rustc_session::{declare_lint_pass, declare_tool_lint};
2021
use rustc_span::source_map::Span;
@@ -1407,7 +1408,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14071408
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
14081409
let item = cx.tcx.hir().expect_item(parent);
14091410
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
1410-
let ty = cx.tcx.type_of(def_id);
1411+
let self_ty = cx.tcx.type_of(def_id);
14111412
if_chain! {
14121413
if let hir::ImplItemKind::Fn(ref sig, id) = impl_item.kind;
14131414
if let Some(first_arg) = iter_input_pats(&sig.decl, cx.tcx.hir().body(id)).next();
@@ -1429,7 +1430,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14291430
if name == method_name &&
14301431
sig.decl.inputs.len() == n_args &&
14311432
out_type.matches(cx, &sig.decl.output) &&
1432-
self_kind.matches(cx, ty, first_arg_ty) {
1433+
self_kind.matches(cx, self_ty, first_arg_ty) {
14331434
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, impl_item.span, &format!(
14341435
"defining a method called `{}` on this type; consider implementing \
14351436
the `{}` trait or choosing a less ambiguous name", name, trait_name));
@@ -1441,7 +1442,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14411442
.iter()
14421443
.find(|(ref conv, _)| conv.check(&name))
14431444
{
1444-
if !self_kinds.iter().any(|k| k.matches(cx, ty, first_arg_ty)) {
1445+
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
14451446
let lint = if item.vis.node.is_pub() {
14461447
WRONG_PUB_SELF_CONVENTION
14471448
} else {
@@ -1471,8 +1472,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14711472
if let hir::ImplItemKind::Fn(_, _) = impl_item.kind {
14721473
let ret_ty = return_ty(cx, impl_item.hir_id);
14731474

1475+
let contains_self_ty = |ty: Ty<'tcx>| {
1476+
ty.walk().any(|inner| match inner.unpack() {
1477+
GenericArgKind::Type(inner_ty) => same_tys(cx, self_ty, inner_ty),
1478+
1479+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
1480+
})
1481+
};
1482+
14741483
// walk the return type and check for Self (this does not check associated types)
1475-
if ret_ty.walk().any(|inner_type| same_tys(cx, ty, inner_type)) {
1484+
if contains_self_ty(ret_ty) {
14761485
return;
14771486
}
14781487

@@ -1486,18 +1495,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14861495
let associated_type = binder.skip_binder();
14871496

14881497
// walk the associated type and check for Self
1489-
for inner_type in associated_type.walk() {
1490-
if same_tys(cx, ty, inner_type) {
1491-
return;
1492-
}
1498+
if contains_self_ty(associated_type) {
1499+
return;
14931500
}
14941501
},
14951502
(_, _) => {},
14961503
}
14971504
}
14981505
}
14991506

1500-
if name == "new" && !same_tys(cx, ret_ty, ty) {
1507+
if name == "new" && !same_tys(cx, ret_ty, self_ty) {
15011508
span_lint(
15021509
cx,
15031510
NEW_RET_NO_SELF,

clippy_lints/src/use_self.rs

+41-50
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::ty;
1414
use rustc_middle::ty::{DefIdTree, Ty};
1515
use rustc_session::{declare_lint_pass, declare_tool_lint};
1616
use rustc_span::symbol::kw;
17+
use rustc_typeck::hir_ty_to_ty;
1718

1819
use crate::utils::{differing_macro_contexts, span_lint_and_sugg};
1920

@@ -80,37 +81,28 @@ fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path<'_>, last_segment: O
8081
);
8182
}
8283

83-
struct TraitImplTyVisitor<'a, 'tcx> {
84-
item_type: Ty<'tcx>,
84+
// FIXME: always use this (more correct) visitor, not just in method signatures.
85+
struct SemanticUseSelfVisitor<'a, 'tcx> {
8586
cx: &'a LateContext<'a, 'tcx>,
86-
trait_type_walker: ty::walk::TypeWalker<'tcx>,
87-
impl_type_walker: ty::walk::TypeWalker<'tcx>,
87+
self_ty: Ty<'tcx>,
8888
}
8989

90-
impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
90+
impl<'a, 'tcx> Visitor<'tcx> for SemanticUseSelfVisitor<'a, 'tcx> {
9191
type Map = Map<'tcx>;
9292

93-
fn visit_ty(&mut self, t: &'tcx hir::Ty<'_>) {
94-
let trait_ty = self.trait_type_walker.next();
95-
let impl_ty = self.impl_type_walker.next();
96-
97-
if_chain! {
98-
if let TyKind::Path(QPath::Resolved(_, path)) = &t.kind;
99-
100-
// The implementation and trait types don't match which means that
101-
// the concrete type was specified by the implementation
102-
if impl_ty != trait_ty;
103-
if let Some(impl_ty) = impl_ty;
104-
if self.item_type == impl_ty;
105-
then {
106-
match path.res {
107-
def::Res::SelfTy(..) => {},
108-
_ => span_use_self_lint(self.cx, path, None)
109-
}
93+
fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'_>) {
94+
if let TyKind::Path(QPath::Resolved(_, path)) = &hir_ty.kind {
95+
match path.res {
96+
def::Res::SelfTy(..) => {},
97+
_ => {
98+
if hir_ty_to_ty(self.cx.tcx, hir_ty) == self.self_ty {
99+
span_use_self_lint(self.cx, path, None);
100+
}
101+
},
110102
}
111103
}
112104

113-
walk_ty(self, t)
105+
walk_ty(self, hir_ty)
114106
}
115107

116108
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
@@ -120,10 +112,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
120112

121113
fn check_trait_method_impl_decl<'a, 'tcx>(
122114
cx: &'a LateContext<'a, 'tcx>,
123-
item_type: Ty<'tcx>,
124115
impl_item: &ImplItem<'_>,
125116
impl_decl: &'tcx FnDecl<'_>,
126-
impl_trait_ref: &ty::TraitRef<'_>,
117+
impl_trait_ref: ty::TraitRef<'tcx>,
127118
) {
128119
let trait_method = cx
129120
.tcx
@@ -134,34 +125,35 @@ fn check_trait_method_impl_decl<'a, 'tcx>(
134125
let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
135126
let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
136127

137-
let impl_method_def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
138-
let impl_method_sig = cx.tcx.fn_sig(impl_method_def_id);
139-
let impl_method_sig = cx.tcx.erase_late_bound_regions(&impl_method_sig);
140-
141-
let output_ty = if let FnRetTy::Return(ty) = &impl_decl.output {
128+
let output_hir_ty = if let FnRetTy::Return(ty) = &impl_decl.output {
142129
Some(&**ty)
143130
} else {
144131
None
145132
};
146133

147-
// `impl_decl_ty` (of type `hir::Ty`) represents the type declared in the signature.
148-
// `impl_ty` (of type `ty:TyS`) is the concrete type that the compiler has determined for
149-
// that declaration. We use `impl_decl_ty` to see if the type was declared as `Self`
150-
// and use `impl_ty` to check its concrete type.
151-
for (impl_decl_ty, (impl_ty, trait_ty)) in impl_decl.inputs.iter().chain(output_ty).zip(
152-
impl_method_sig
153-
.inputs_and_output
154-
.iter()
155-
.zip(trait_method_sig.inputs_and_output),
156-
) {
157-
let mut visitor = TraitImplTyVisitor {
158-
cx,
159-
item_type,
160-
trait_type_walker: trait_ty.walk(),
161-
impl_type_walker: impl_ty.walk(),
162-
};
163-
164-
visitor.visit_ty(&impl_decl_ty);
134+
// `impl_hir_ty` (of type `hir::Ty`) represents the type written in the signature.
135+
// `trait_ty` (of type `ty::Ty`) is the semantic type for the signature in the trait.
136+
// We use `impl_hir_ty` to see if the type was written as `Self`,
137+
// `hir_ty_to_ty(...)` to check semantic types of paths, and
138+
// `trait_ty` to determine which parts of the signature in the trait, mention
139+
// the type being implemented verbatim (as opposed to `Self`).
140+
for (impl_hir_ty, trait_ty) in impl_decl
141+
.inputs
142+
.iter()
143+
.chain(output_hir_ty)
144+
.zip(trait_method_sig.inputs_and_output)
145+
{
146+
// Check if the input/output type in the trait method specifies the implemented
147+
// type verbatim, and only suggest `Self` if that isn't the case.
148+
// This avoids suggestions to e.g. replace `Vec<u8>` with `Vec<Self>`,
149+
// in an `impl Trait for u8`, when the trait always uses `Vec<u8>`.
150+
// See also https://github.com/rust-lang/rust-clippy/issues/2894.
151+
let self_ty = impl_trait_ref.self_ty();
152+
if !trait_ty.walk().any(|inner| inner == self_ty.into()) {
153+
let mut visitor = SemanticUseSelfVisitor { cx, self_ty };
154+
155+
visitor.visit_ty(&impl_hir_ty);
156+
}
165157
}
166158
}
167159

@@ -197,8 +189,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
197189
let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
198190
if let ImplItemKind::Fn(FnSig{ decl: impl_decl, .. }, impl_body_id)
199191
= &impl_item.kind {
200-
let item_type = cx.tcx.type_of(impl_def_id);
201-
check_trait_method_impl_decl(cx, item_type, impl_item, impl_decl, &impl_trait_ref);
192+
check_trait_method_impl_decl(cx, impl_item, impl_decl, impl_trait_ref);
202193

203194
let body = cx.tcx.hir().body(*impl_body_id);
204195
visitor.visit_body(body);

setup-toolchain.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
# Set up the appropriate rustc toolchain
33

44
set -e

0 commit comments

Comments
 (0)