- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi All,
I recently had a bug in some code due to a variable getting redefined when I would have guessed it should have been flagged as a name conflict compilation error. Here is an example of the code:
Since I rely heavily on implicit none, I often don't focus on checking on variable name uniqueness. In the above case, "temp" in the globalV module was being used for temperature, while in the subroutine, it was just a "temp" variable.
I don't understand why the above code does not cause an error. Can someone elaborate why it shouldn't?
Thanks
-joe
I recently had a bug in some code due to a variable getting redefined when I would have guessed it should have been flagged as a name conflict compilation error. Here is an example of the code:
[fortran]module globalV implicit none real temp end module globalV module system use globalV ! if this is commented out, then compile errors are reported; otherwise not. implicit none contains subroutine sub (a) use globalV real a real temp ! why doesn't this cause a compilation error? a = temp return end subroutine sub end module system program test use globalV use system implicit none real a call sub (a) stop end program test[/fortran]If the "use globalV" statement is removed at the top of "module system", then the compiler properly flags the second declaration of variable temp as an error. However, when the use statement occurs as shown, no compiler or run errors occur.
Since I rely heavily on implicit none, I often don't focus on checking on variable name uniqueness. In the above case, "temp" in the globalV module was being used for temperature, while in the subroutine, it was just a "temp" variable.
I don't understand why the above code does not cause an error. Can someone elaborate why it shouldn't?
Thanks
-joe
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, this is interesting. The outer USE establishes TEMP as a local identifier in the scope of module SYSTEM. Inside SUB, TEMP is "host associated" and your declaration of TEMP as a variable overrides the host-associated name. This is the way the language is defined, but it can sometimes get you into trouble - see the "Up Periscope" section of Doctor Fortran in "Too Much of a Good Thing?"
When you have the USE only inside of SUB, then TEMP is use-associated and the language forbids you to declare another local identifier of the same name, so this is why you get an error when the outer USE is removed.
What puzzles me is that you get no error with both USE lines. It's as if the compiler is ignoring the second one because all the names are already defined. This may be a bug - I will have to study the standard to see.
When you have the USE only inside of SUB, then TEMP is use-associated and the language forbids you to declare another local identifier of the same name, so this is why you get an error when the outer USE is removed.
What puzzles me is that you get no error with both USE lines. It's as if the compiler is ignoring the second one because all the names are already defined. This may be a bug - I will have to study the standard to see.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the ref. Steve; it was useful. I think I will start using USE statements inside proceedures instead of relying on the host association, which basically caused my problem.
-joe
-joe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Reported as issue DPD200171846.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page