Code compiles with nvfortran but fails with gfortran - derived type not recognized

I have a simple Fortran program with a derived type (TYPE). It compiles and runs perfectly with nvfortran, but gfortran gives me compilation errors about the derived type not being defined.

module classmodule
implicit none
type :: car
     character(10) :: color
     real          :: fuel
end type car

contains
subroutine createcar(car, color, fuel)
  type(car), intent(out) :: car
  character(*), intent(in) :: color
  real, intent(in) :: fuel
  car%color = color
  car%fuel = fuel
  print*, color , fuel
end subroutine createcar

end module classmodule

program test_derived_type
  use classmodule
  implicit none

  type(car) :: mycar
  call createcar(mycar, "red", 50.0)

end program test_derived_type

$ gfortran --version
GNU Fortran (Ubuntu 15.2.0-16ubuntu1) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gfortran test.f90 -o test
test.f90:10:12:

10 | type(car), intent(out) :: car
| 1
Error: Derived type ‘car’ at (1) is being used before it is defined
test.f90:13:7:

13 | car%color = color
| 1
Error: Symbol ‘car’ at (1) has no IMPLICIT type
test.f90:14:7:

14 | car%fuel = fuel
| 1
Error: Symbol ‘car’ at (1) has no IMPLICIT type
test.f90:9:22:

9 | subroutine createcar(car, color, fuel)
  |                      1~~

Error: Symbol ‘car’ at (1) has no IMPLICIT type
test.f90:21:7:

21 | use classmodule
| 1
Fatal Error: Cannot open module file ‘classmodule.mod’ for reading at (1): No such file or directory
compilation terminated.

with nvfortran

$ nvfortran test.f90 -o test
$ ./test
red 50.00000

With windows gfortran

C:\Users\Shahid\Desktop>gfortran test.f90 -o test
test.f90:10:12:

10 | type(car), intent(out) :: car
| 1
Error: Derived type ‘car’ at (1) is being used before it is defined
test.f90:13:7:

13 | car%color = color
| 1
Error: Symbol ‘car’ at (1) has no IMPLICIT type
test.f90:14:7:

14 | car%fuel = fuel
| 1
Error: Symbol ‘car’ at (1) has no IMPLICIT type
test.f90:9:22:

9 | subroutine createcar(car, color, fuel)
  |                      1~~

Error: Symbol ‘car’ at (1) has no IMPLICIT type
test.f90:21:7:

21 | use classmodule
| 1
Fatal Error: Cannot open module file ‘classmodule.mod’ for reading at (1): No such file or directory
compilation terminated.

I think the problem is that you have a derived type and a variable with the same name. Just change the dummy argument within the subroutine, and everything works as expected. BTW, flang and nagfor also complain about the code. Nvfortran seems to be the odd man out.

I (and I’m sure several others) try to avoid this problem by appending a _t to the derived type name to signal the variable is a derived type, ie

type :: car_t

end type

type(car_t) :: car

Also if want to differentiate between standard derived types and types with type bound procedures (ie a class) you could also append a _c

Just a suggestion