@@ -118,6 +118,105 @@ fn test_generate_files_resolves_modules_without_an_ast() {
118118 assert run_result.output.trim_space () == '42'
119119}
120120
121+ fn test_header_discovers_imports_only_from_selected_comptime_branches () {
122+ root := os.join_path (os.vtmp_dir (), 'v3_fastc_comptime_imports_${os .getpid ()}' )
123+ os.rmdir_all (root) or {}
124+ os.mkdir_all (os.join_path (root, 'alpha' )) or { panic (err) }
125+ os.mkdir_all (os.join_path (root, 'beta' )) or { panic (err) }
126+ defer {
127+ os.rmdir_all (root) or {}
128+ }
129+ main_file := os.join_path (root, 'main.v' )
130+ os.write_file (main_file, 'module main
131+
132+ \$ if linux {
133+ import alpha as dep
134+ } \$ else {
135+ import beta as dep
136+ }
137+
138+ fn main() {
139+ dep.ping()
140+ }
141+ ' ) or {
142+ panic (err)
143+ }
144+ os.write_file (os.join_path (root, 'alpha' , 'alpha.v' ), "module alpha
145+
146+ fn init() {
147+ println('alpha init')
148+ }
149+
150+ fn cleanup() {
151+ println('alpha cleanup')
152+ }
153+
154+ pub fn ping() {}
155+ " ) or {
156+ panic (err)
157+ }
158+ os.write_file (os.join_path (root, 'beta' , 'beta.v' ), "module beta
159+
160+ fn init() {
161+ println('beta init')
162+ }
163+
164+ fn cleanup() {
165+ println('beta cleanup')
166+ }
167+
168+ pub fn ping() {}
169+ " ) or {
170+ panic (err)
171+ }
172+ mut prefs := pref.new_preferences ()
173+ prefs.target = pref.target_from ('linux' , pref.host_arch ()) or { panic (err) }
174+ prefs.module_search_paths = [root]
175+ header := fastc_scan_source_header (os.read_file (main_file) or { panic (err) }, main_file, prefs) or {
176+ panic (err)
177+ }
178+ assert header.import_order == ['alpha' ]
179+ assert header.imports['dep' ] == 'alpha'
180+ assert 'beta' ! in header.imports.values ()
181+ sources := fastc_resolve_source_files ([main_file], prefs) or { panic (err) }
182+ mut resolved_modules := []string {}
183+ for source_file in sources {
184+ if source_file.header.module_name ! in resolved_modules {
185+ resolved_modules << source_file.header.module_name
186+ }
187+ }
188+ assert resolved_modules == ['main' , 'alpha' ]
189+ prefs.building_v = true
190+ c_source := generate_source_files (sources, prefs) or { panic (err) }
191+ assert c_source.contains ('\t alpha__init();' ), c_source
192+ assert c_source.contains ('\t alpha__cleanup();' ), c_source
193+ assert ! c_source.contains ('beta__init' ), c_source
194+ assert ! c_source.contains ('beta__cleanup' ), c_source
195+ }
196+
197+ fn test_generate_files_rejects_mismatched_imported_module_declarations () {
198+ root := os.join_path (os.vtmp_dir (), 'v3_fastc_module_mismatch_${os .getpid ()}' )
199+ os.rmdir_all (root) or {}
200+ os.mkdir_all (os.join_path (root, 'foo' )) or { panic (err) }
201+ defer {
202+ os.rmdir_all (root) or {}
203+ }
204+ main_file := os.join_path (root, 'main.v' )
205+ os.write_file (main_file, 'module main\n import foo\n fn main() { println(foo.answer()) }\n ' ) or {
206+ panic (err)
207+ }
208+ os.write_file (os.join_path (root, 'foo' , 'foo.v' ),
209+ 'module bar\n pub fn answer() int { return 42 }\n ' ) or { panic (err) }
210+ mut prefs := pref.new_preferences ()
211+ prefs.module_search_paths = [root]
212+ mut message := ''
213+ _ := generate_files ([main_file], prefs) or {
214+ message = err.msg ()
215+ ''
216+ }
217+ assert message.contains ('declares module `bar` instead of `foo`' ), message
218+ }
219+
121220fn test_generate_files_preserves_all_blank_imports () {
122221 root := os.join_path (os.vtmp_dir (), 'v3_fastc_blank_imports_${os .getpid ()}' )
123222 os.rmdir_all (root) or {}
0 commit comments