parser: allow embedding interfaces from other modules (#13385)
							parent
							
								
									1dc239227d
								
							
						
					
					
						commit
						10dcb2e0d9
					
				|  | @ -519,6 +519,32 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { | |||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		// Check embedded interface from external module
 | ||||
| 		if p.tok.kind == .name && p.peek_tok.kind == .dot { | ||||
| 			if p.tok.lit !in p.imports { | ||||
| 				p.error_with_pos('mod `$p.tok.lit` not imported', p.tok.pos()) | ||||
| 				break | ||||
| 			} | ||||
| 			mod_name := p.tok.lit | ||||
| 			from_mod_typ := p.parse_type() | ||||
| 			from_mod_name := '${mod_name}.$p.prev_tok.lit' | ||||
| 			if from_mod_name.is_lower() { | ||||
| 				p.error_with_pos('The interface name need to have the pascal case', p.prev_tok.pos()) | ||||
| 				break | ||||
| 			} | ||||
| 			comments := p.eat_comments() | ||||
| 			ifaces << ast.InterfaceEmbedding{ | ||||
| 				name: from_mod_name | ||||
| 				typ: from_mod_typ | ||||
| 				pos: p.prev_tok.pos() | ||||
| 				comments: comments | ||||
| 			} | ||||
| 			if p.tok.kind == .rcbr { | ||||
| 				break | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if p.tok.kind == .key_mut { | ||||
| 			if is_mut { | ||||
| 				p.error_with_pos('redefinition of `mut` section', p.tok.pos()) | ||||
|  |  | |||
|  | @ -0,0 +1,15 @@ | |||
| module main | ||||
| 
 | ||||
| import interface_from_another_module.mod | ||||
| 
 | ||||
| interface IBar { | ||||
| 	mod.IFoo | ||||
| } | ||||
| 
 | ||||
| struct Abc {} | ||||
| 
 | ||||
| fn test_interface() { | ||||
| 	a := IBar(Abc{}) | ||||
| 	dump(a) | ||||
| 	assert true | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| module mod | ||||
| 
 | ||||
| pub interface IFoo {} | ||||
		Loading…
	
		Reference in New Issue