fail_in_nested_scope.carbon 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/parse/testdata/packages/package/fail_in_nested_scope.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/packages/package/fail_in_nested_scope.carbon
  10. // api.carbon
  11. class C {
  12. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE+7]]:3: error: the `package` declaration must be the first non-comment line [PackageTooLate]
  13. // CHECK:STDERR: package P;
  14. // CHECK:STDERR: ^~~~~~~
  15. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE-4]]:1: note: first non-comment line is here [FirstNonCommentLine]
  16. // CHECK:STDERR: class C {
  17. // CHECK:STDERR: ^~~~~
  18. // CHECK:STDERR:
  19. package P;
  20. }
  21. fn F() {
  22. // `package` here is an expression naming the package scope.
  23. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE+4]]:11: error: expected `.` after `package` expression [ExpectedPeriodAfterPackage]
  24. // CHECK:STDERR: package P;
  25. // CHECK:STDERR: ^
  26. // CHECK:STDERR:
  27. package P;
  28. // OK
  29. package.P;
  30. }
  31. // impl.carbon
  32. class C {
  33. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE+7]]:3: error: the `package` declaration must be the first non-comment line [PackageTooLate]
  34. // CHECK:STDERR: impl package P;
  35. // CHECK:STDERR: ^~~~
  36. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE-29]]:1: note: first non-comment line is here [FirstNonCommentLine]
  37. // CHECK:STDERR: class C {
  38. // CHECK:STDERR: ^~~~~
  39. // CHECK:STDERR:
  40. impl package P;
  41. }
  42. fn F() {
  43. // This reaches decl parsing because `impl` is a valid modifier or introducer.
  44. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE+7]]:3: error: the `package` declaration must be the first non-comment line [PackageTooLate]
  45. // CHECK:STDERR: impl package P;
  46. // CHECK:STDERR: ^~~~
  47. // CHECK:STDERR: fail_in_nested_scope.carbon:[[@LINE-41]]:1: note: first non-comment line is here [FirstNonCommentLine]
  48. // CHECK:STDERR: class C {
  49. // CHECK:STDERR: ^~~~~
  50. // CHECK:STDERR:
  51. impl package P;
  52. }
  53. // CHECK:STDOUT: - filename: fail_in_nested_scope.carbon
  54. // CHECK:STDOUT: parse_tree: [
  55. // CHECK:STDOUT: {kind: 'FileStart', text: ''},
  56. // CHECK:STDOUT: {kind: 'ClassIntroducer', text: 'class'},
  57. // CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'C'},
  58. // CHECK:STDOUT: {kind: 'ClassDefinitionStart', text: '{', subtree_size: 3},
  59. // CHECK:STDOUT: {kind: 'PackageIntroducer', text: 'package'},
  60. // CHECK:STDOUT: {kind: 'PackageDecl', text: ';', has_error: yes, subtree_size: 2},
  61. // CHECK:STDOUT: {kind: 'ClassDefinition', text: '}', subtree_size: 6},
  62. // CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
  63. // CHECK:STDOUT: {kind: 'IdentifierNameBeforeParams', text: 'F'},
  64. // CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
  65. // CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 2},
  66. // CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
  67. // CHECK:STDOUT: {kind: 'PackageExpr', text: 'package'},
  68. // CHECK:STDOUT: {kind: 'ExprStatement', text: ';', has_error: yes, subtree_size: 2},
  69. // CHECK:STDOUT: {kind: 'PackageExpr', text: 'package'},
  70. // CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'P'},
  71. // CHECK:STDOUT: {kind: 'MemberAccessExpr', text: '.', subtree_size: 3},
  72. // CHECK:STDOUT: {kind: 'ExprStatement', text: ';', subtree_size: 4},
  73. // CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 12},
  74. // CHECK:STDOUT: {kind: 'ClassIntroducer', text: 'class'},
  75. // CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'C'},
  76. // CHECK:STDOUT: {kind: 'ClassDefinitionStart', text: '{', subtree_size: 3},
  77. // CHECK:STDOUT: {kind: 'PackageIntroducer', text: 'package'},
  78. // CHECK:STDOUT: {kind: 'ImplModifier', text: 'impl'},
  79. // CHECK:STDOUT: {kind: 'PackageDecl', text: ';', has_error: yes, subtree_size: 3},
  80. // CHECK:STDOUT: {kind: 'ClassDefinition', text: '}', subtree_size: 7},
  81. // CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
  82. // CHECK:STDOUT: {kind: 'IdentifierNameBeforeParams', text: 'F'},
  83. // CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
  84. // CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 2},
  85. // CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
  86. // CHECK:STDOUT: {kind: 'PackageIntroducer', text: 'package'},
  87. // CHECK:STDOUT: {kind: 'ImplModifier', text: 'impl'},
  88. // CHECK:STDOUT: {kind: 'PackageDecl', text: ';', has_error: yes, subtree_size: 3},
  89. // CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 9},
  90. // CHECK:STDOUT: {kind: 'FileEnd', text: ''},
  91. // CHECK:STDOUT: ]