Skip to content

Conversation

@kubajj
Copy link
Contributor

@kubajj kubajj commented Mar 24, 2021

Create a new subroutine called right at the end of the build model subroutine. This subroutine loops through modules provided by each source file and checks for duplicates.

@kubajj kubajj marked this pull request as ready for review March 24, 2021 22:48
Copy link
Member

@milancurcic milancurcic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kubajj! I left a few suggestions.

@LKedward LKedward linked an issue Mar 25, 2021 that may be closed by this pull request
Copy link
Member

@LKedward LKedward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thanks Jakub @kubajj, in addition to Milan's comments I spotted a few more things (see my comments).

@kubajj kubajj requested a review from milancurcic March 26, 2021 22:53
@kubajj
Copy link
Contributor Author

kubajj commented Mar 26, 2021

During the monthly call, you were talking about rebasing and squashing. Should I do it?

@ivan-pi
Copy link
Member

ivan-pi commented Mar 27, 2021

Should this PR include a test case with definition of duplicate modules? @LKedward

@LKedward
Copy link
Member

Looking good so far @kubajj! I don't think there's any pressing need to rebase this branch currently.

Should this PR include a test case with definition of duplicate modules? @LKedward

Agreed yes we should put some unit tests in for this.

@kubajj do you want to have a look at test_module_dependencies.f90; this testsuite provides some helpers to mock up package sources very easily and it should be quite straightforward to add a few new tests for this duplicates check. Try to test the various possible scenarios (duplicates in same package, in different packages, in different source types, in same file, no duplicates etc.). Let me know if you need some more explanation of the testsuite. Otherwise I'm happy to also just commit some tests straight to your branch.

Co-authored-by: Laurence Kedward <[email protected]>
@kubajj
Copy link
Contributor Author

kubajj commented Mar 30, 2021

Looking good so far @kubajj! I don't think there's any pressing need to rebase this branch currently.

Should this PR include a test case with definition of duplicate modules? @LKedward

Agreed yes we should put some unit tests in for this.

@kubajj do you want to have a look at test_module_dependencies.f90; this testsuite provides some helpers to mock up package sources very easily and it should be quite straightforward to add a few new tests for this duplicates check. Try to test the various possible scenarios (duplicates in same package, in different packages, in different source types, in same file, no duplicates etc.). Let me know if you need some more explanation of the testsuite. Otherwise I'm happy to also just commit some tests straight to your branch.

@LKedward For creating the different scenarios, should I add source files that would have duplicate modules and load them as in subroutine test_library_module_use or is there another way how to do it?

@LKedward
Copy link
Member

No need to write actual fortran source files for these tests, just like the other tests in test_module_dependencies.f90 you can use the new_test_source helper to create a source object with certain modules.

Here's an example unit test to use:

    subroutine test_package_module_duplicates(error)
        
        type(error_t), allocatable, intent(out) :: error

        type(fpm_model_t) :: model
        logical :: duplicates_found

        allocate(model%packages(1))
        allocate(model%packages(1)%sources(2))

        model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
                                    scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])

        model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/subdir/my_mod_1.f90", &
                                    scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])

        call check_modules_for_duplicates(model, duplicates_found)
        if (duplicates_found) then
            call test_failed(error,'Duplicate modules found')
            return
        end if
    end subroutine test_package_module_duplicates

(The filename here doesn't matter, the file doesn't need to exist, it is used for other tests.)

Then you just need to add test_package_module_duplicates to the collect routine at the top with should_fail=.true..

@kubajj
Copy link
Contributor Author

kubajj commented Mar 30, 2021

@LKedward Oh, this is brilliant. What did you mean by different source types in the list of possible scenarios?

Copy link
Member

@LKedward LKedward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work Jakub @kubajj. This all looks good 👍 I've left two minor comments, once those are addressed I think this will be good to merge!

What did you mean by different source types in the list of possible scenarios?

I think I meant like FPM_UNIT_MODULE/FPM_UNIT_PROGRAM etc. but don't worry I don't think that matters for these changes. The tests added here look good as they are.

fpm/src/fpm.f90 Outdated
if (allocated(model%packages(k)%sources(l)%modules_provided)) then
do m=1,size(model%packages(k)%sources(l)%modules_provided)
if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules(:modi-1)) then
write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is a duplicate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to also print out which file the duplicate has been found in here
(model%packages(k)%sources(l)%file_name)

Comment on lines 47 to 48
& new_unittest("package-with-duplicates-in-same-source", &
test_package_module_duplicates_same_source, should_fail=.true.), &
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
& new_unittest("package-with-duplicates-in-same-source", &
test_package_module_duplicates_same_source, should_fail=.true.), &

I think these lines have been copy-pasted accidentally

@kubajj kubajj changed the title Draft - Fix for Issue #396 - Duplicate module definitions Fix for Issue #396 - Duplicate module definitions Mar 31, 2021
Copy link
Member

@awvwgk awvwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@awvwgk
Copy link
Member

awvwgk commented Mar 31, 2021

I'll go ahead and merge before we start pruning the Haskell version from this repository.

@awvwgk awvwgk merged commit 5422ec5 into fortran-lang:master Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Throw error message for duplicate module definitions

5 participants