-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Extend QueryStability
to handle IntoIterator
implementations
#139345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smoelius
wants to merge
11
commits into
rust-lang:master
Choose a base branch
from
smoelius:into-iter-stability
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−52
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
08ce2d0
Extend `QueryStability` to handle `IntoIterator` implementations
smoelius 15866b6
Fix adjacent code
smoelius c102394
Fix duplicate warning; merge test into `tests/ui-fulldeps/internal-li…
smoelius af5e468
Use `rustc_middle::ty::FnSig::inputs`
smoelius 5fa3384
Address two review comments
smoelius fe2458b
Use `Instance::try_resolve`
smoelius 76d82dc
Import `rustc_middle::ty::Ty` as `Ty` rather than `MiddleTy`
smoelius e8b2a43
Simplify predicate handling
smoelius cdcc78a
Add more `#[allow(rustc::potential_query_instability)]` following rebase
smoelius cd4f539
Remove two `#[allow(rustc::potential_query_instability)]` following r…
smoelius ba9c93e
Address review comment
smoelius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,10 +1,10 @@ | ||||||||
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as | ||||||||
//! Clippy. | ||||||||
use rustc_hir::HirId; | ||||||||
use rustc_hir::def::Res; | ||||||||
use rustc_hir::def_id::DefId; | ||||||||
use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy}; | ||||||||
use rustc_hir::{Expr, ExprKind, HirId}; | ||||||||
use rustc_middle::ty::{self, ClauseKind, GenericArgsRef, PredicatePolarity, TraitPredicate, Ty}; | ||||||||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||||||||
use rustc_span::hygiene::{ExpnKind, MacroKind}; | ||||||||
use rustc_span::{Span, sym}; | ||||||||
|
@@ -56,25 +56,6 @@ impl LateLintPass<'_> for DefaultHashTypes { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
/// Helper function for lints that check for expressions with calls and use typeck results to | ||||||||
/// get the `DefId` and `GenericArgsRef` of the function. | ||||||||
fn typeck_results_of_method_fn<'tcx>( | ||||||||
cx: &LateContext<'tcx>, | ||||||||
expr: &hir::Expr<'_>, | ||||||||
) -> Option<(Span, DefId, ty::GenericArgsRef<'tcx>)> { | ||||||||
match expr.kind { | ||||||||
hir::ExprKind::MethodCall(segment, ..) | ||||||||
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) => | ||||||||
{ | ||||||||
Some((segment.ident.span, def_id, cx.typeck_results().node_args(expr.hir_id))) | ||||||||
} | ||||||||
_ => match cx.typeck_results().node_type(expr.hir_id).kind() { | ||||||||
&ty::FnDef(def_id, args) => Some((expr.span, def_id, args)), | ||||||||
_ => None, | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
declare_tool_lint! { | ||||||||
/// The `potential_query_instability` lint detects use of methods which can lead to | ||||||||
/// potential query instability, such as iterating over a `HashMap`. | ||||||||
|
@@ -101,10 +82,12 @@ declare_tool_lint! { | |||||||
|
||||||||
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]); | ||||||||
|
||||||||
impl LateLintPass<'_> for QueryStability { | ||||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { | ||||||||
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return }; | ||||||||
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, args) | ||||||||
impl<'tcx> LateLintPass<'tcx> for QueryStability { | ||||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||||||||
if let Some((callee_def_id, span, generic_args, _recv, _args)) = | ||||||||
get_callee_span_generic_args_and_args(cx, expr) | ||||||||
&& let Ok(Some(instance)) = | ||||||||
ty::Instance::try_resolve(cx.tcx, cx.typing_env(), callee_def_id, generic_args) | ||||||||
{ | ||||||||
let def_id = instance.def_id(); | ||||||||
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) { | ||||||||
|
@@ -113,6 +96,13 @@ impl LateLintPass<'_> for QueryStability { | |||||||
span, | ||||||||
QueryInstability { query: cx.tcx.item_name(def_id) }, | ||||||||
); | ||||||||
} else if has_unstable_into_iter_predicate(cx, callee_def_id, generic_args) { | ||||||||
let call_span = span.with_hi(expr.span.hi()); | ||||||||
cx.emit_span_lint( | ||||||||
POTENTIAL_QUERY_INSTABILITY, | ||||||||
call_span, | ||||||||
QueryInstability { query: sym::into_iter }, | ||||||||
); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if cx.tcx.has_attr(def_id, sym::rustc_lint_untracked_query_information) { | ||||||||
cx.emit_span_lint( | ||||||||
|
@@ -125,6 +115,64 @@ impl LateLintPass<'_> for QueryStability { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
fn has_unstable_into_iter_predicate<'tcx>( | ||||||||
cx: &LateContext<'tcx>, | ||||||||
callee_def_id: DefId, | ||||||||
generic_args: GenericArgsRef<'tcx>, | ||||||||
) -> bool { | ||||||||
let Some(into_iterator_def_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator) else { | ||||||||
lcnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return false; | ||||||||
}; | ||||||||
let Some(into_iter_fn_def_id) = cx.tcx.lang_items().into_iter_fn() else { | ||||||||
return false; | ||||||||
}; | ||||||||
let predicates = cx.tcx.predicates_of(callee_def_id).instantiate(cx.tcx, generic_args); | ||||||||
for (predicate, _) in predicates { | ||||||||
let ClauseKind::Trait(TraitPredicate { trait_ref, polarity: PredicatePolarity::Positive }) = | ||||||||
predicate.kind().skip_binder() | ||||||||
else { | ||||||||
continue; | ||||||||
}; | ||||||||
// Does the function or method require any of its arguments to implement `IntoIterator`? | ||||||||
if trait_ref.def_id != into_iterator_def_id { | ||||||||
continue; | ||||||||
} | ||||||||
let Ok(Some(instance)) = | ||||||||
ty::Instance::try_resolve(cx.tcx, cx.typing_env(), into_iter_fn_def_id, trait_ref.args) | ||||||||
else { | ||||||||
continue; | ||||||||
}; | ||||||||
// Does the input type's `IntoIterator` implementation have the | ||||||||
// `rustc_lint_query_instability` attribute on its `into_iter` method? | ||||||||
if cx.tcx.has_attr(instance.def_id(), sym::rustc_lint_query_instability) { | ||||||||
return true; | ||||||||
} | ||||||||
} | ||||||||
false | ||||||||
} | ||||||||
|
||||||||
/// Checks whether an expression is a function or method call and, if so, returns its `DefId`, | ||||||||
/// `Span`, `GenericArgs`, and arguments. This is a slight augmentation of a similarly named Clippy | ||||||||
/// function, `get_callee_generic_args_and_args`. | ||||||||
fn get_callee_span_generic_args_and_args<'tcx>( | ||||||||
cx: &LateContext<'tcx>, | ||||||||
expr: &'tcx Expr<'tcx>, | ||||||||
) -> Option<(DefId, Span, GenericArgsRef<'tcx>, Option<&'tcx Expr<'tcx>>, &'tcx [Expr<'tcx>])> { | ||||||||
if let ExprKind::Call(callee, args) = expr.kind | ||||||||
&& let callee_ty = cx.typeck_results().expr_ty(callee) | ||||||||
&& let ty::FnDef(callee_def_id, generic_args) = callee_ty.kind() | ||||||||
{ | ||||||||
return Some((*callee_def_id, callee.span, generic_args, None, args)); | ||||||||
} | ||||||||
if let ExprKind::MethodCall(segment, recv, args, _) = expr.kind | ||||||||
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) | ||||||||
{ | ||||||||
let generic_args = cx.typeck_results().node_args(expr.hir_id); | ||||||||
return Some((method_def_id, segment.ident.span, generic_args, Some(recv), args)); | ||||||||
} | ||||||||
None | ||||||||
} | ||||||||
|
||||||||
declare_tool_lint! { | ||||||||
/// The `usage_of_ty_tykind` lint detects usages of `ty::TyKind::<kind>`, | ||||||||
/// where `ty::<kind>` would suffice. | ||||||||
|
@@ -461,33 +509,22 @@ declare_tool_lint! { | |||||||
declare_lint_pass!(Diagnostics => [UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL]); | ||||||||
|
||||||||
impl LateLintPass<'_> for Diagnostics { | ||||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { | ||||||||
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { | ||||||||
let collect_args_tys_and_spans = |args: &[hir::Expr<'_>], reserve_one_extra: bool| { | ||||||||
let mut result = Vec::with_capacity(args.len() + usize::from(reserve_one_extra)); | ||||||||
result.extend(args.iter().map(|arg| (cx.typeck_results().expr_ty(arg), arg.span))); | ||||||||
result | ||||||||
}; | ||||||||
// Only check function calls and method calls. | ||||||||
let (span, def_id, fn_gen_args, arg_tys_and_spans) = match expr.kind { | ||||||||
hir::ExprKind::Call(callee, args) => { | ||||||||
match cx.typeck_results().node_type(callee.hir_id).kind() { | ||||||||
&ty::FnDef(def_id, fn_gen_args) => { | ||||||||
(callee.span, def_id, fn_gen_args, collect_args_tys_and_spans(args, false)) | ||||||||
} | ||||||||
_ => return, // occurs for fns passed as args | ||||||||
} | ||||||||
} | ||||||||
hir::ExprKind::MethodCall(_segment, _recv, args, _span) => { | ||||||||
let Some((span, def_id, fn_gen_args)) = typeck_results_of_method_fn(cx, expr) | ||||||||
else { | ||||||||
return; | ||||||||
}; | ||||||||
let mut args = collect_args_tys_and_spans(args, true); | ||||||||
args.insert(0, (cx.tcx.types.self_param, _recv.span)); // dummy inserted for `self` | ||||||||
(span, def_id, fn_gen_args, args) | ||||||||
} | ||||||||
_ => return, | ||||||||
let Some((def_id, span, fn_gen_args, recv, args)) = | ||||||||
get_callee_span_generic_args_and_args(cx, expr) | ||||||||
else { | ||||||||
return; | ||||||||
}; | ||||||||
let mut arg_tys_and_spans = collect_args_tys_and_spans(args, recv.is_some()); | ||||||||
if let Some(recv) = recv { | ||||||||
arg_tys_and_spans.insert(0, (cx.tcx.types.self_param, recv.span)); // dummy inserted for `self` | ||||||||
} | ||||||||
|
||||||||
Self::diagnostic_outside_of_impl(cx, span, expr.hir_id, def_id, fn_gen_args); | ||||||||
Self::untranslatable_diagnostic(cx, def_id, &arg_tys_and_spans); | ||||||||
|
@@ -496,7 +533,7 @@ impl LateLintPass<'_> for Diagnostics { | |||||||
|
||||||||
impl Diagnostics { | ||||||||
// Is the type `{D,Subd}iagMessage`? | ||||||||
fn is_diag_message<'cx>(cx: &LateContext<'cx>, ty: MiddleTy<'cx>) -> bool { | ||||||||
fn is_diag_message<'cx>(cx: &LateContext<'cx>, ty: Ty<'cx>) -> bool { | ||||||||
if let Some(adt_def) = ty.ty_adt_def() | ||||||||
&& let Some(name) = cx.tcx.get_diagnostic_name(adt_def.did()) | ||||||||
&& matches!(name, sym::DiagMessage | sym::SubdiagMessage) | ||||||||
|
@@ -510,7 +547,7 @@ impl Diagnostics { | |||||||
fn untranslatable_diagnostic<'cx>( | ||||||||
cx: &LateContext<'cx>, | ||||||||
def_id: DefId, | ||||||||
arg_tys_and_spans: &[(MiddleTy<'cx>, Span)], | ||||||||
arg_tys_and_spans: &[(Ty<'cx>, Span)], | ||||||||
) { | ||||||||
let fn_sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder(); | ||||||||
let predicates = cx.tcx.predicates_of(def_id).instantiate_identity(cx.tcx).predicates; | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.