<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Feb 5, 2014 at 12:43 PM, Ben Langmuir <span dir="ltr"><<a href="mailto:blangmuir@apple.com" target="_blank">blangmuir@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On Feb 5, 2014, at 11:18 AM, Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:<br>
<br>
> I'm not especially fond of treating annot_module_begin and annot_module_include differently. That'll mean we get completely different diagnostics for a '#include' in a bad place depending on whether it's including something in the same top-level module or something in a different top-level module.<br>

><br>
<br>
</div>I approached this from the other side and didn't like that annot_module_include is handled so differently from @import.</blockquote><div><br></div><div>I think they're fundamentally different, at least currently:</div>
<div><br></div><div>#include sometimes triggers a submodule build (in the same preprocessor + parser + sema) so must only appear at the top level in such a case. We also want the #include to behave (effectively) the same whether it textually includes a file or imports a module, so that people don't accidentally write code that means different things with compilers that support modules and with compilers that do not (or depending on which module a particular header is part of, or the order in which submodules of a module are built).</div>
<div><br></div><div>Conversely, @import merely makes declaration and macro names visible. It never triggers an in-situ submodule build (sometimes a different module will be built in a separate thread and context, but that's not a problem). It can't (reliably) be used to import a submodule of the same module. So, there's no *technical* reason not to allow @import anywhere, as far as I can tell. That said, some of these properties are probably bugs in @import, and maybe it should act more like #include.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> I'm also not sure that the diagnostic change here is an improvement. Previously:<br>
><br>
> #include "not_module.h"<br>
> #include "module.h"<br>
><br>
> ... where not_module.h contains:<br>
><br>
> class X {<br>
>   // ...<br>
> // whoops, forgot the '};'<br>
><br>
> would give a "missing '}'" diagnostic, pointing at the '{' that is left un-closed. It now gives a mysterious 'must be at the top level' diagnostic, not pointing at the '}' any more, followed by more errors (because 'module.h' didn't get imported and because the code after the #include is still within the class). The error here was almost certainly that a '}' was missing, so this seems like a regression.<br>

<br>
</div>Ick, I’ll take a look at this case.<br>
<div class="im"><br>
><br>
> In what cases does this change allow us to recover from errors better?<br>
<br>
<br>
</div>Whenever an implicit import is not at top level, we currently give errors like ‘expected expression’, or ‘expected }’, pointing at the location on #include/#import, and give no hint as to how a preprocessor directive (#include) could be the source of the error.</blockquote>
<div><br></div><div>We can, and almost certainly should, customize the ExpectAndConsume diagnostic for the case where the current token is one of the annot_module_* tokens. I think that's probably the best way to attack this issue.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">To make matters worse, once you break out of whatever parsing context you were in due to the module_include, any following code is likely to become errors.<br>

<br>
int foo() {<br>
  int x = 5;<br>
  #import “Bar.h” // expected }<br>
  if (x == 6) {// expected identifier or (<br>
    ++x; // expected identifier or (<br>
  }<br>
  return 1; // expected identifier of (<br>
} // extraneous closing brace</blockquote><div><br></div><div>Do you think this is actually a likely error case? I find it implausible that someone would deliberately include a modular header into the middle of some scope. I think it's vastly more likely that they missed a close brace before the include.</div>
<div><br></div><div>If this really is a likely scenario, then I think we should make the diagnostic for an unexpected annot_module_* a FatalError, because there's simply no way we can reasonably recover (we have two recovery options, and either of them is disastrous if we're in the other situation).</div>
</div></div></div>