# Why does the syntax highlighter color a local symbol red inside DynamicModule?

I am adding a Module inside DynamicModule here is an example (thanks to @rm-rf for this simple example)

DynamicModule[{x = 1}, Module[{x}, x = 2] ];


This Module has a local symbol x which happens also to be the name of another local symbol in the enclosing DynamicModule.

Yet, the front end is making all occurrences of x as red inside the Module. Here is a screenshot:

The question is: Why would local symbols to one Module conflict with local symbols of the enclosing DynamcModule? Yet, the code runs with no problem. So, could this just be that the FE is confused? Or am I doing something wrong?

-
Your example is needlessly complicated and makes it harder to convey the point... here's a simple one: DynamicModule[{x = 1}, Module[{x}, x = 2] ]; –  rm -rf Feb 24 '13 at 4:09
or maybe even DynamicModule[{x}, {x, Module[{x}, x]}] to see that the variables are different. –  Mike Honeychurch Feb 24 '13 at 4:45
what i meant in the previous comment was evaluate the input DynamicModule[{x}, {x, Module[{x}, x]}] and observe the output. –  Mike Honeychurch Feb 24 '13 at 6:20

I think it's just a warning that you have a symbol inside a localization scope that contains a symbol of the same (base) name. Compare

With[{max = 2}, {With[{max = 3}, max], max}]

Module[{max}, max = 2; {Module[{max}, max = 3], max}]

Block[{max}, max = 2; {Module[{max}, max = 3], max}]


and so on. The outputs above are each {3,2} and no messages are generated, but the inside maxs are red. A bit annoying, perhaps.

If you look at the menu command Help > Why the Coloring... the palette indicates a local scope conflict. It's not an error, but a possible mistake. (It's a common mistake for beginners, probably.)

Here is an answer to a related question.

Update

Whether to color local scope conflicts and other warning, and what color to use, can be set with the menu command Preferences > Appearance > Errors and Warnings, which is mentioned in the answer mentioned above.

-
I agree with Michael E2. It would appear that the Mathematica code editor's syntax analyzer isn't up handling these nested constructs. I suppose it might be considered a bug, but like Michael I take it as a warning. –  m_goldberg Feb 24 '13 at 3:30
@Nasser There are several examples of the highlighter showing something as red when it's not really an error. I'm also not surprised that it can't know well in advance that this won't lead to an error, so better safe than sorry. The only true answer to your question is to treat it as a warning and proceed if you know what you're doing. –  rm -rf Feb 24 '13 at 3:35
@Nasser You get the same red on the equal sign with If[x = y,...]`, which is not an error either. It's often just a mistake. –  Michael E2 Feb 24 '13 at 3:36
@Nasser, they won't clash but can you picture a situation where you used the same name in both nested modules by mistake and then tried to refer to the outermost variable with that common name? Just a warning –  Rojo Feb 24 '13 at 4:06
@Nasser, then open Edit, Preferences, Appearance, Error and Warnings, and uncheck the warnings you don't like –  Rojo Feb 24 '13 at 5:11