@@ -46,6 +46,13 @@ use super::instantiate::TyVarCache;
4646use super :: instantiate_spec:: ParamKind ;
4747use super :: { ControlKind , MethodContext , ParamSpec , TraitImpl , TypeContext } ;
4848
49+ type ClassTrait < ' c > = ( Type , Option < ( Type , & ' c TypeSpecWithOp ) > ) ;
50+ type ClassTraitErrors < ' c > = (
51+ Option < Type > ,
52+ Option < ( Type , & ' c TypeSpecWithOp ) > ,
53+ TyCheckErrors ,
54+ ) ;
55+
4956pub fn valid_mod_name ( name : & str ) -> bool {
5057 !name. is_empty ( ) && !name. starts_with ( '/' ) && name. trim ( ) == name
5158}
@@ -987,7 +994,7 @@ impl Context {
987994 pub ( crate ) fn get_class_and_impl_trait < ' c > (
988995 & mut self ,
989996 class_spec : & ' c ast:: TypeSpec ,
990- ) -> TyCheckResult < ( Type , Option < ( Type , & ' c TypeSpecWithOp ) > ) > {
997+ ) -> Result < ClassTrait < ' c > , ClassTraitErrors < ' c > > {
991998 let mut errs = TyCheckErrors :: empty ( ) ;
992999 let mut dummy_tv_cache = TyVarCache :: new ( self . level , self ) ;
9931000 match class_spec {
@@ -1013,14 +1020,21 @@ impl Context {
10131020 ( t, & tasc. t_spec )
10141021 }
10151022 other => {
1016- return Err ( TyCheckErrors :: from ( TyCheckError :: syntax_error (
1017- self . cfg . input . clone ( ) ,
1018- line ! ( ) as usize ,
1019- other. loc ( ) ,
1020- self . caused_by ( ) ,
1021- format ! ( "expected type ascription, but found {}" , other. name( ) ) ,
1023+ return Err ( (
1024+ None ,
10221025 None ,
1023- ) ) )
1026+ TyCheckErrors :: from ( TyCheckError :: syntax_error (
1027+ self . cfg . input . clone ( ) ,
1028+ line ! ( ) as usize ,
1029+ other. loc ( ) ,
1030+ self . caused_by ( ) ,
1031+ format ! (
1032+ "expected type ascription, but found {}" ,
1033+ other. name( )
1034+ ) ,
1035+ None ,
1036+ ) ) ,
1037+ ) )
10241038 }
10251039 } ;
10261040 let class = match self . instantiate_typespec_full (
@@ -1039,7 +1053,7 @@ impl Context {
10391053 if errs. is_empty ( ) {
10401054 Ok ( ( class, Some ( ( impl_trait, t_spec) ) ) )
10411055 } else {
1042- Err ( errs)
1056+ Err ( ( Some ( class ) , Some ( ( impl_trait , t_spec ) ) , errs) )
10431057 }
10441058 }
10451059 ast:: TypeAppArgsKind :: SubtypeOf ( trait_spec) => {
@@ -1052,8 +1066,10 @@ impl Context {
10521066 ) {
10531067 Ok ( t) => t,
10541068 Err ( ( t, es) ) => {
1055- errs. extend ( es) ;
1056- t
1069+ if !PYTHON_MODE {
1070+ errs. extend ( es) ;
1071+ }
1072+ t. replace ( & Type :: Failure , & Type :: Never )
10571073 }
10581074 } ;
10591075 let class = match self . instantiate_typespec_full (
@@ -1072,7 +1088,7 @@ impl Context {
10721088 if errs. is_empty ( ) {
10731089 Ok ( ( class, Some ( ( impl_trait, trait_spec. as_ref ( ) ) ) ) )
10741090 } else {
1075- Err ( errs)
1091+ Err ( ( Some ( class ) , Some ( ( impl_trait , trait_spec . as_ref ( ) ) ) , errs) )
10761092 }
10771093 }
10781094 }
@@ -1094,7 +1110,7 @@ impl Context {
10941110 if errs. is_empty ( ) {
10951111 Ok ( ( t, None ) )
10961112 } else {
1097- Err ( errs)
1113+ Err ( ( Some ( t ) , None , errs) )
10981114 }
10991115 }
11001116 }
@@ -1243,10 +1259,14 @@ impl Context {
12431259 . instantiate_vis_modifier ( class_def. def . sig . vis ( ) )
12441260 . unwrap_or ( VisibilityModifier :: Public ) ;
12451261 for methods in class_def. methods_list . iter ( ) {
1246- let Ok ( ( class, impl_trait) ) = self . get_class_and_impl_trait ( & methods. class )
1247- else {
1248- continue ;
1249- } ;
1262+ let ( class, impl_trait) =
1263+ match self . get_class_and_impl_trait ( & methods. class ) {
1264+ Ok ( x) => x,
1265+ Err ( ( class, trait_, errs) ) => {
1266+ total_errs. extend ( errs) ;
1267+ ( class. unwrap_or ( Type :: Obj ) , trait_)
1268+ }
1269+ } ;
12501270 // assume the class has implemented the trait, regardless of whether the implementation is correct
12511271 if let Some ( ( trait_, trait_loc) ) = & impl_trait {
12521272 if let Err ( errs) = self . register_trait_impl ( & class, trait_, * trait_loc)
0 commit comments