@@ -18,7 +18,7 @@ use std::fmt;
1818use std:: sync:: Arc ;
1919
2020use crate :: chalk:: lowering:: { self , LowerInto } ;
21- use rustc_hir :: Unsafety ;
21+ use rustc_ast :: ast ;
2222
2323pub struct RustIrDatabase < ' tcx > {
2424 pub ( crate ) interner : RustInterner < ' tcx > ,
@@ -247,14 +247,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
247247 } ;
248248 Arc :: new ( chalk_solve:: rust_ir:: FnDefDatum {
249249 id : fn_def_id,
250- sig : chalk_ir:: FnSig {
251- abi : sig. abi ( ) ,
252- safety : match sig. unsafety ( ) {
253- Unsafety :: Normal => chalk_ir:: Safety :: Safe ,
254- Unsafety :: Unsafe => chalk_ir:: Safety :: Unsafe ,
255- } ,
256- variadic : sig. c_variadic ( ) ,
257- } ,
250+ sig : sig. lower_into ( & self . interner ) ,
258251 binders : chalk_ir:: Binders :: new ( binders, bound) ,
259252 } )
260253 }
@@ -329,21 +322,75 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
329322 fn impl_provided_for (
330323 & self ,
331324 auto_trait_id : chalk_ir:: TraitId < RustInterner < ' tcx > > ,
332- adt_id : chalk_ir:: AdtId < RustInterner < ' tcx > > ,
325+ app_ty : & chalk_ir:: ApplicationTy < RustInterner < ' tcx > > ,
333326 ) -> bool {
327+ use chalk_ir:: Scalar :: * ;
328+ use chalk_ir:: TypeName :: * ;
329+
334330 let trait_def_id = auto_trait_id. 0 ;
335- let adt_def = adt_id. 0 ;
336331 let all_impls = self . interner . tcx . all_impls ( trait_def_id) ;
337332 for impl_def_id in all_impls {
338333 let trait_ref = self . interner . tcx . impl_trait_ref ( impl_def_id) . unwrap ( ) ;
339334 let self_ty = trait_ref. self_ty ( ) ;
340- match * self_ty. kind ( ) {
341- ty:: Adt ( impl_adt_def, _) => {
342- if impl_adt_def == adt_def {
343- return true ;
335+ let provides = match ( self_ty. kind ( ) , app_ty. name ) {
336+ ( & ty:: Adt ( impl_adt_def, ..) , Adt ( id) ) => impl_adt_def. did == id. 0 . did ,
337+ ( _, AssociatedType ( _ty_id) ) => {
338+ // FIXME(chalk): See https://github.com/rust-lang/rust/pull/77152#discussion_r494484774
339+ false
340+ }
341+ ( ty:: Bool , Scalar ( Bool ) ) => true ,
342+ ( ty:: Char , Scalar ( Char ) ) => true ,
343+ ( ty:: Int ( ty1) , Scalar ( Int ( ty2) ) ) => match ( ty1, ty2) {
344+ ( ast:: IntTy :: Isize , chalk_ir:: IntTy :: Isize )
345+ | ( ast:: IntTy :: I8 , chalk_ir:: IntTy :: I8 )
346+ | ( ast:: IntTy :: I16 , chalk_ir:: IntTy :: I16 )
347+ | ( ast:: IntTy :: I32 , chalk_ir:: IntTy :: I32 )
348+ | ( ast:: IntTy :: I64 , chalk_ir:: IntTy :: I64 )
349+ | ( ast:: IntTy :: I128 , chalk_ir:: IntTy :: I128 ) => true ,
350+ _ => false ,
351+ } ,
352+ ( ty:: Uint ( ty1) , Scalar ( Uint ( ty2) ) ) => match ( ty1, ty2) {
353+ ( ast:: UintTy :: Usize , chalk_ir:: UintTy :: Usize )
354+ | ( ast:: UintTy :: U8 , chalk_ir:: UintTy :: U8 )
355+ | ( ast:: UintTy :: U16 , chalk_ir:: UintTy :: U16 )
356+ | ( ast:: UintTy :: U32 , chalk_ir:: UintTy :: U32 )
357+ | ( ast:: UintTy :: U64 , chalk_ir:: UintTy :: U64 )
358+ | ( ast:: UintTy :: U128 , chalk_ir:: UintTy :: U128 ) => true ,
359+ _ => false ,
360+ } ,
361+ ( ty:: Float ( ty1) , Scalar ( Float ( ty2) ) ) => match ( ty1, ty2) {
362+ ( ast:: FloatTy :: F32 , chalk_ir:: FloatTy :: F32 )
363+ | ( ast:: FloatTy :: F64 , chalk_ir:: FloatTy :: F64 ) => true ,
364+ _ => false ,
365+ } ,
366+ ( & ty:: Tuple ( ..) , Tuple ( ..) ) => true ,
367+ ( & ty:: Array ( ..) , Array ) => true ,
368+ ( & ty:: Slice ( ..) , Slice ) => true ,
369+ ( & ty:: RawPtr ( type_and_mut) , Raw ( mutability) ) => {
370+ match ( type_and_mut. mutbl , mutability) {
371+ ( ast:: Mutability :: Mut , chalk_ir:: Mutability :: Mut ) => true ,
372+ ( ast:: Mutability :: Mut , chalk_ir:: Mutability :: Not ) => false ,
373+ ( ast:: Mutability :: Not , chalk_ir:: Mutability :: Mut ) => false ,
374+ ( ast:: Mutability :: Not , chalk_ir:: Mutability :: Not ) => true ,
344375 }
345376 }
346- _ => { }
377+ ( & ty:: Ref ( .., mutability1) , Ref ( mutability2) ) => match ( mutability1, mutability2) {
378+ ( ast:: Mutability :: Mut , chalk_ir:: Mutability :: Mut ) => true ,
379+ ( ast:: Mutability :: Mut , chalk_ir:: Mutability :: Not ) => false ,
380+ ( ast:: Mutability :: Not , chalk_ir:: Mutability :: Mut ) => false ,
381+ ( ast:: Mutability :: Not , chalk_ir:: Mutability :: Not ) => true ,
382+ } ,
383+ ( & ty:: Opaque ( def_id, ..) , OpaqueType ( opaque_ty_id) ) => def_id == opaque_ty_id. 0 ,
384+ ( & ty:: FnDef ( def_id, ..) , FnDef ( fn_def_id) ) => def_id == fn_def_id. 0 ,
385+ ( & ty:: Str , Str ) => true ,
386+ ( & ty:: Never , Never ) => true ,
387+ ( & ty:: Closure ( def_id, ..) , Closure ( closure_id) ) => def_id == closure_id. 0 ,
388+ ( & ty:: Foreign ( def_id) , Foreign ( foreign_def_id) ) => def_id == foreign_def_id. 0 ,
389+ ( & ty:: Error ( ..) , Error ) => false ,
390+ _ => false ,
391+ } ;
392+ if provides {
393+ return true ;
347394 }
348395 }
349396 false
@@ -418,16 +465,18 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
418465 well_known_trait : chalk_solve:: rust_ir:: WellKnownTrait ,
419466 ) -> Option < chalk_ir:: TraitId < RustInterner < ' tcx > > > {
420467 use chalk_solve:: rust_ir:: WellKnownTrait :: * ;
468+ let lang_items = self . interner . tcx . lang_items ( ) ;
421469 let def_id = match well_known_trait {
422- Sized => self . interner . tcx . lang_items ( ) . sized_trait ( ) ,
423- Copy => self . interner . tcx . lang_items ( ) . copy_trait ( ) ,
424- Clone => self . interner . tcx . lang_items ( ) . clone_trait ( ) ,
425- Drop => self . interner . tcx . lang_items ( ) . drop_trait ( ) ,
426- Fn => self . interner . tcx . lang_items ( ) . fn_trait ( ) ,
427- FnMut => self . interner . tcx . lang_items ( ) . fn_mut_trait ( ) ,
428- FnOnce => self . interner . tcx . lang_items ( ) . fn_once_trait ( ) ,
429- Unsize => self . interner . tcx . lang_items ( ) . unsize_trait ( ) ,
430- Unpin => self . interner . tcx . lang_items ( ) . unpin_trait ( ) ,
470+ Sized => lang_items. sized_trait ( ) ,
471+ Copy => lang_items. copy_trait ( ) ,
472+ Clone => lang_items. clone_trait ( ) ,
473+ Drop => lang_items. drop_trait ( ) ,
474+ Fn => lang_items. fn_trait ( ) ,
475+ FnMut => lang_items. fn_mut_trait ( ) ,
476+ FnOnce => lang_items. fn_once_trait ( ) ,
477+ Unsize => lang_items. unsize_trait ( ) ,
478+ Unpin => lang_items. unpin_trait ( ) ,
479+ CoerceUnsized => lang_items. coerce_unsized_trait ( ) ,
431480 } ;
432481 def_id. map ( chalk_ir:: TraitId )
433482 }
0 commit comments