-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix ICE 6840 - make is_normalizable more strict #6866
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,7 @@ use rustc_trait_selection::traits::query::normalize::AtExt; | |
use smallvec::SmallVec; | ||
|
||
use crate::consts::{constant, Constant}; | ||
use std::collections::HashMap; | ||
|
||
pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Option<RustcVersion> { | ||
if let Ok(version) = RustcVersion::parse(msrv) { | ||
|
@@ -1488,10 +1489,42 @@ pub fn match_function_call<'tcx>( | |
/// Checks if `Ty` is normalizable. This function is useful | ||
/// to avoid crashes on `layout_of`. | ||
pub fn is_normalizable<'tcx>(cx: &LateContext<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool { | ||
cx.tcx.infer_ctxt().enter(|infcx| { | ||
is_normalizable_helper(cx, param_env, ty, &mut HashMap::new()) | ||
} | ||
|
||
fn is_normalizable_helper<'tcx>( | ||
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. One last thing: This function should not have to exist, if the |
||
cx: &LateContext<'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
ty: Ty<'tcx>, | ||
cache: &mut HashMap<Ty<'tcx>, bool>, | ||
) -> bool { | ||
if let Some(&cached_result) = cache.get(ty) { | ||
return cached_result; | ||
} | ||
cache.insert(ty, false); // prevent recursive loops | ||
flip1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let result = cx.tcx.infer_ctxt().enter(|infcx| { | ||
let cause = rustc_middle::traits::ObligationCause::dummy(); | ||
infcx.at(&cause, param_env).normalize(ty).is_ok() | ||
}) | ||
if infcx.at(&cause, param_env).normalize(ty).is_ok() { | ||
match ty.kind() { | ||
ty::Adt(def, substs) => def.variants.iter().all(|variant| { | ||
variant | ||
.fields | ||
.iter() | ||
.all(|field| is_normalizable_helper(cx, param_env, field.ty(cx.tcx, substs), cache)) | ||
}), | ||
_ => ty.walk().all(|generic_arg| match generic_arg.unpack() { | ||
flip1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GenericArgKind::Type(inner_ty) if inner_ty != ty => { | ||
is_normalizable_helper(cx, param_env, inner_ty, cache) | ||
}, | ||
_ => true, // if inner_ty == ty, we've already checked it | ||
}), | ||
} | ||
} else { | ||
false | ||
} | ||
}); | ||
cache.insert(ty, result); | ||
result | ||
} | ||
|
||
pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -> bool { | ||
|
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! This is a reproducer for the ICE 6840: https://github.com/rust-lang/rust-clippy/issues/6840. | ||
//! The ICE is caused by `TyCtxt::layout_of` and `is_normalizable` not being strict enough | ||
#![allow(dead_code)] | ||
use std::collections::HashMap; | ||
|
||
pub trait Rule { | ||
type DependencyKey; | ||
} | ||
|
||
pub struct RuleEdges<R: Rule> { | ||
dependencies: R::DependencyKey, | ||
} | ||
|
||
type RuleDependencyEdges<R> = HashMap<u32, RuleEdges<R>>; | ||
|
||
// reproducer from the GitHub issue ends here | ||
// but check some additional variants | ||
type RuleDependencyEdgesArray<R> = HashMap<u32, [RuleEdges<R>; 8]>; | ||
type RuleDependencyEdgesSlice<R> = HashMap<u32, &'static [RuleEdges<R>]>; | ||
type RuleDependencyEdgesRef<R> = HashMap<u32, &'static RuleEdges<R>>; | ||
type RuleDependencyEdgesRaw<R> = HashMap<u32, *const RuleEdges<R>>; | ||
type RuleDependencyEdgesTuple<R> = HashMap<u32, (RuleEdges<R>, RuleEdges<R>)>; | ||
|
||
// and an additional checks to make sure fix doesn't have stack-overflow issue | ||
// on self-containing types | ||
pub struct SelfContaining { | ||
inner: Box<SelfContaining>, | ||
} | ||
type SelfContainingEdges = HashMap<u32, SelfContaining>; | ||
|
||
fn main() {} |
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.