Use TypeckResults::expr_ty instead of TyCtxt::type_of to fix "Not a type" ICE

This commit is contained in:
Yoshitomo Nakanishi 2021-02-27 21:52:15 +09:00
parent 7154b23601
commit bdeec5dbd6
3 changed files with 23 additions and 5 deletions

View file

@ -130,10 +130,9 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
}
},
ExprKind::Struct(qpath, fields, base) => {
ExprKind::Struct(_, fields, base) => {
if_chain! {
if let Some(def_id) = self.cx.qpath_res(qpath, expr.hir_id).opt_def_id();
let ty = self.cx.tcx.type_of(def_id);
let ty = self.cx.typeck_results().expr_ty(expr);
if let Some(adt_def) = ty.ty_adt_def();
if adt_def.is_struct();
if let Some(variant) = adt_def.variants.iter().next();

View file

@ -66,8 +66,7 @@ impl LateLintPass<'_> for InconsistentStructConstructor {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if_chain! {
if let ExprKind::Struct(qpath, fields, base) = expr.kind;
if let Some(def_id) = cx.qpath_res(qpath, expr.hir_id).opt_def_id();
let ty = cx.tcx.type_of(def_id);
let ty = cx.typeck_results().expr_ty(expr);
if let Some(adt_def) = ty.ty_adt_def();
if adt_def.is_struct();
if let Some(variant) = adt_def.variants.iter().next();

View file

@ -0,0 +1,20 @@
//! This is a reproducer for the ICE 6792: https://github.com/rust-lang/rust-clippy/issues/6792.
//! The ICE is caused by using `TyCtxt::type_of(assoc_type_id)`.
trait Trait {
type Ty;
fn broken() -> Self::Ty;
}
struct Foo {}
impl Trait for Foo {
type Ty = Foo;
fn broken() -> Self::Ty {
Self::Ty {}
}
}
fn main() {}