# Messages leak out from Block

Bug introduced in 8.0 or earlier and fixed in 10.1

When assigning message to a symbol localized by Block, I would expect that this message is not accessible outside of Block, but I observe different behavior:

ClearAll[x]
Block[{x}, x::test1 = "message1"];
x::test1
(* "message1" *)


Message was assigned to x globally regardless of x being localized.

It also happens with direct assignment to Messages:

ClearAll[x]
Block[{x}, Messages[x] = {HoldPattern[x::test2] :> "message2"}];
x::test2
(* "message2" *)


I get this behavior in all versions I have available: 8.0.0, 9.0.1 and 10.0.0 on Linux.

Is this a bug?

• I think this happens because MessageName has certain auto-stringification step, which allows the second argument of MessageName to be a symbol. When the definition is constructed, it is auto-stringified, but then still attached to the symbol. From the point of view of what Block is supposed to do, this can be probably considered a bug. – Leonid Shifrin Feb 7 '15 at 13:21
• @LeonidShifrin But it also happens when directly assigning to Messages list. Is MessageName involved also in this kind of assignment? – jkuczm Feb 7 '15 at 13:24
• I think it has to. May be not MessageName specifically, but I think that the underlying mechanism used to attach message(s) to symbol(s) should be the same in both cases. And it is this mechanism whose interaction with Block looks problematic. – Leonid Shifrin Feb 7 '15 at 14:06
• @Leonid Are you comfortable adding the bugs tag to this one? – Mr.Wizard Feb 8 '15 at 15:23
• @Mr.Wizard I think so. I just did. – Leonid Shifrin Feb 8 '15 at 15:29

This has been fixed in 10.1 (windows)

code

ClearAll[x]
Block[{x}, x::test1 = "message1"];
x::test1

ClearAll[x]
Block[{x}, Messages[x] = {HoldPattern[x::test2] :> "message2"}];
x::test2

Block[{x}, x /: x::foo = "bar"; Message[x::foo]];
Messages[x]


My initial answer was simply wrong. I now think this is a bug. Although a message may be expressly attached to the Symbol x using TagSet it is still not localized. (TagSet is superfluous but it helps make the point.)

Block[{x}, x /: x::foo = "bar"; Message[x::foo]];

Messages[x]


During evaluation of In[]:= x::foo: bar

{HoldPattern[x::foo] :> "bar"}


In fact attempting to attach the rule to MessageName yields this:

MessageName /: x::foo = "bar";


MessageName::tag: Rule for MessageName of x::foo can only be attached to x. >>

• But Subscript has no "automagic" defined like MessageName. Assigning to MessageName really assigns to symbols Messages and it also happens when assigning directly to Messages which I expected to behave like DownValues etc. – jkuczm Feb 7 '15 at 11:15
• @jkuczm Nevertheless the underlying cause may be the same, in that the definition is not attached to the Symbol x. I am working on this now. Sorry for the premature answer. – Mr.Wizard Feb 7 '15 at 11:16
• I thought it should assign to x. LanguageExtendedDefinition[x] gives among others Messages list with message assigned inside Block`. – jkuczm Feb 7 '15 at 11:18
• @jkuczm Good point. Answer rewritten. – Mr.Wizard Feb 7 '15 at 11:20