Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions c2rust-refactor/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::ops::Deref;

use rustc_ast::ptr::P;
use rustc_ast::{
Expr, ExprKind, FnDecl, FnRetTy, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId, Path,
QSelf, UseTreeKind, DUMMY_NODE_ID,
AssocItem, Expr, ExprKind, FnDecl, FnRetTy, ForeignItem, ForeignItemKind, Item, ItemKind,
NodeId, Path, QSelf, UseTreeKind, DUMMY_NODE_ID,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{DiagnosticBuilder, Level};
Expand Down Expand Up @@ -1073,6 +1073,16 @@ impl<'a, 'tcx, 'b> TypeCompare<'a, 'tcx, 'b> {
pub fn compatible_types(&self, item1: &Item, item2: &Item, match_vis: bool) -> bool {
use rustc_ast::ItemKind::*;
match (&item1.kind, &item2.kind) {
(Impl(box ref impl1), Impl(box ref impl2)) => {
if impl1.items.len() != impl2.items.len() {
return false;
}

(impl1.items.iter())
.zip(impl2.items.iter())
.all(|(item1, item2)| self.compatible_assoc_items(item1, item2, match_vis))
}

// * Assure that these two items are in fact of the same type, just to be safe.
(TyAlias(box ref ta1), TyAlias(box ref ta2)) => {
match (
Expand Down Expand Up @@ -1227,6 +1237,40 @@ impl<'a, 'tcx, 'b> TypeCompare<'a, 'tcx, 'b> {
}
}

pub fn compatible_assoc_items(
&self,
item1: &AssocItem,
item2: &AssocItem,
match_vis: bool,
) -> bool {
use rustc_ast::AssocItemKind::*;

// Unlike for regular items, associated items must also match by name.
if item1.ident.as_str() != item2.ident.as_str() {
return false;
}

match (&item1.kind, &item2.kind) {
(Const(def1, ty1, expr1), Const(def2, ty2, expr2)) => match (
self.cx.opt_node_type(item1.id),
self.cx.opt_node_type(item2.id),
) {
(Some(ty1), Some(ty2)) => {
self.structural_eq_tys(ty1, ty2)
&& expr1.unnamed_equiv(expr2)
&& def1.unnamed_equiv(def2)
}
_ => {
self.structural_eq_ast_tys(ty1, ty2, match_vis)
&& expr1.unnamed_equiv(expr2)
&& def1.unnamed_equiv(def2)
}
},

_ => false,
}
}

/// Compare two function declarations for equivalent argument and return types,
/// ignoring argument names.
pub fn compatible_fn_prototypes(&self, decl1: &FnDecl, decl2: &FnDecl) -> bool {
Expand Down
Loading
Loading