reject_init_in_main.mx raw

   1  // expected: init() is not allowed in main package
   2  //
   3  // A main package's setup belongs in main(), which is the program entry; an
   4  // init() there would run before it with no defined place in the entry
   5  // sequence. Legacy says so, and stage4 now rejects it too: it used to print
   6  // "init() is not allowed" and skip the function, so the program built and the
   7  // init never ran - a silent difference, not a rejection.
   8  //
   9  // A library package may have exactly one init(), which runs after its
  10  // package-level variable initializers and before any importer's.
  11  //
  12  // Note on legacy: its package-declaration checks are gated on the shape of the
  13  // package path, so legacy rejects this file when the build root resolves to a
  14  // module path but accepts the same source copied to a bare out-of-tree
  15  // directory (whose path has no dot). stage4 applies the rule by package kind,
  16  // which is what the rule says; the restriction harness copies test sources to
  17  // an out-of-tree directory, so this one is intentionally stage4-only.
  18  package main
  19  
  20  func init() {
  21  	println("setup")
  22  }
  23  
  24  func main() {
  25  	println("body")
  26  }
  27