Skip to content

Commit

Permalink
Move is_normalizable into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed Jan 2, 2020
1 parent 20318e0 commit bf67fcf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 4 additions & 7 deletions clippy_lints/src/transmute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg};
use crate::utils::{
is_normalizable, last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg,
};
use if_chain::if_chain;
use rustc::declare_lint_pass;
use rustc::hir::*;
Expand Down Expand Up @@ -641,12 +643,7 @@ fn get_type_snippet(cx: &LateContext<'_, '_>, path: &QPath<'_>, to_ref_ty: Ty<'_
fn is_layout_incompatible<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
let empty_param_env = ty::ParamEnv::empty();
// check if `from` and `to` are normalizable to avoid ICE (#4968)
let is_normalizable = cx.tcx.infer_ctxt().enter(|infcx| {
let cause = rustc::traits::ObligationCause::dummy();
infcx.at(&cause, empty_param_env).normalize(&from).is_ok()
&& infcx.at(&cause, empty_param_env).normalize(&to).is_ok()
});
if !is_normalizable {
if !(is_normalizable(cx, empty_param_env, from) && is_normalizable(cx, empty_param_env, to)) {
return false;
}
let from_ty_layout = cx.tcx.layout_of(empty_param_env.and(from));
Expand Down
9 changes: 9 additions & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,15 @@ pub fn match_function_call<'a, 'tcx>(
None
}

/// Checks if `Ty` is normalizable. This function is useful
/// to avoid crashes on `layout_of`.
pub fn is_normalizable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
cx.tcx.infer_ctxt().enter(|infcx| {
let cause = rustc::traits::ObligationCause::dummy();
infcx.at(&cause, param_env).normalize(&ty).is_ok()
})
}

#[cfg(test)]
mod test {
use super::{trim_multiline, without_block_comments};
Expand Down

0 comments on commit bf67fcf

Please sign in to comment.