1. my role X::Comp { ... }
  2. my class X::ControlFlow { ... }
  3. my class Exception {
  4. has $!ex;
  5. has $!bt;
  6. method backtrace(Exception:D:) {
  7. if $!bt { $!bt }
  8. elsif nqp::isconcrete($!ex) {
  9. $!bt := Backtrace.new($!ex);
  10. }
  11. else { '' }
  12. }
  13. # Only valid if .backtrace has not been called yet
  14. method vault-backtrace(Exception:D:) {
  15. nqp::isconcrete($!ex) && $!bt ?? Backtrace.new($!ex) !! ''
  16. }
  17. method reset-backtrace(Exception:D:) {
  18. $!ex := Nil
  19. }
  20. multi method Str(Exception:D:) {
  21. my $str;
  22. if nqp::isconcrete($!ex) {
  23. my str $message = nqp::getmessage($!ex);
  24. $str = nqp::isnull_s($message) ?? '' !! nqp::p6box_s($message);
  25. }
  26. $str ||= (try self.?message);
  27. $str = ~$str if defined $str;
  28. $str // "Something went wrong in {self.WHAT.gist}";
  29. }
  30. multi method gist(Exception:D:) {
  31. my $str;
  32. if nqp::isconcrete($!ex) {
  33. my str $message = nqp::getmessage($!ex);
  34. $str = nqp::isnull_s($message)
  35. ?? (try self.?message) // "Died with {self.^name}"
  36. !! nqp::p6box_s($message);
  37. $str ~= "\n";
  38. try $str ~= self.backtrace
  39. || Backtrace.new()
  40. || ' (no backtrace available)';
  41. }
  42. else {
  43. $str = (try self.?message) // "Unthrown {self.^name} with no message";
  44. }
  45. $str;
  46. }
  47. method throw(Exception:D: $bt?) {
  48. $!ex := nqp::newexception() unless nqp::isconcrete($!ex) and $bt;
  49. $!bt := $bt; # Even if !$bt
  50. nqp::setpayload($!ex, nqp::decont(self));
  51. nqp::throw($!ex)
  52. }
  53. method rethrow(Exception:D:) {
  54. unless nqp::isconcrete($!ex) {
  55. $!ex := nqp::newexception();
  56. try nqp::setmessage($!ex, self.message);
  57. }
  58. nqp::setpayload($!ex, nqp::decont(self));
  59. nqp::rethrow($!ex)
  60. }
  61. method resume(Exception:D: --> True) {
  62. nqp::resume($!ex);
  63. }
  64. method die(Exception:D:) { self.throw }
  65. method fail(Exception:D:) {
  66. try self.throw;
  67. my $fail := Failure.new($!);
  68. nqp::throwpayloadlexcaller(nqp::const::CONTROL_RETURN, $fail);
  69. CATCH { $fail.exception.throw }
  70. }
  71. method is-compile-time(--> False) { }
  72. }
  73. my class X::SecurityPolicy is Exception {}
  74. my class X::SecurityPolicy::Eval is X::SecurityPolicy {
  75. has $.payload = "EVAL is a very dangerous function!!!";
  76. my role SlurpySentry { }
  77. method message() {
  78. do {
  79. # Remove spaces for die(*@msg)/fail(*@msg) forms
  80. given $.payload {
  81. when SlurpySentry {
  82. $_.list.join;
  83. }
  84. default {
  85. .Str;
  86. }
  87. }
  88. } ~ " (use the MONKEY-SEE-NO-EVAL pragma to override this error,\n"
  89. ~ "but only if you're VERY sure your data contains no injection attacks)";
  90. }
  91. method Numeric() { $.payload.Numeric }
  92. method from-slurpy (|cap) {
  93. self.new(:payload(cap does SlurpySentry))
  94. }
  95. }
  96. my class X::AdHoc is Exception {
  97. has $.payload = "Unexplained error";
  98. my role SlurpySentry { }
  99. method message() {
  100. # Remove spaces for die(*@msg)/fail(*@msg) forms
  101. given $.payload {
  102. when SlurpySentry {
  103. $_.list.join;
  104. }
  105. default {
  106. .Str;
  107. }
  108. }
  109. }
  110. method Numeric() { $.payload.Numeric }
  111. method from-slurpy (|cap) {
  112. self.new(:payload(cap does SlurpySentry))
  113. }
  114. }
  115. my class X::NQP::NotFound is Exception {
  116. has $.op;
  117. method message() {
  118. "Could not find nqp::$.op, did you forget 'use nqp;' ?"
  119. }
  120. }
  121. my class X::Dynamic::NotFound is Exception {
  122. has $.name;
  123. method message() {
  124. "Dynamic variable $.name not found";
  125. }
  126. }
  127. my class X::Method::NotFound is Exception {
  128. has Mu $.invocant;
  129. has $.method;
  130. has $.typename;
  131. has Bool $.private = False;
  132. method message() {
  133. my $message = $.private
  134. ?? "No such private method '!$.method' for invocant of type '$.typename'"
  135. !! "No such method '$.method' for invocant of type '$.typename'";
  136. my %suggestions;
  137. my int $max_length = do given $.method.chars {
  138. when 0..3 { 1 }
  139. when 4..8 { 2 }
  140. when 9..* { 3 }
  141. }
  142. if $.method eq 'length' {
  143. given $!invocant {
  144. when List { %suggestions{$_} = 0 for <elems> }
  145. when Cool { %suggestions{$_} = 0 for <chars codes>; }
  146. default { %suggestions{$_} = 0 for <elems chars codes>; }
  147. }
  148. }
  149. elsif $.method eq 'bytes' {
  150. %suggestions<encode($encoding).bytes> = 0;
  151. }
  152. if nqp::can($!invocant.HOW, 'methods') {
  153. for $!invocant.^methods(:all)>>.name -> $method_name {
  154. my $dist = StrDistance.new(:before($.method), :after($method_name));
  155. if $dist <= $max_length {
  156. %suggestions{$method_name} = $dist;
  157. }
  158. }
  159. }
  160. if nqp::can($!invocant.HOW, 'private_method_table') {
  161. for $!invocant.^private_method_table.keys -> $method_name {
  162. my $dist = StrDistance.new(:before($.method), :after($method_name));
  163. if $dist <= $max_length {
  164. %suggestions{"!$method_name"} = $dist;
  165. }
  166. }
  167. }
  168. if +%suggestions == 1 {
  169. $message ~= ". Did you mean '%suggestions.keys()'?";
  170. }
  171. elsif +%suggestions > 1 {
  172. $message ~= ". Did you mean any of these?\n { %suggestions.sort(*.value)>>.key.head(4).join("\n ") }\n";
  173. }
  174. $message;
  175. }
  176. }
  177. my class X::Method::InvalidQualifier is Exception {
  178. has $.method;
  179. has $.invocant;
  180. has $.qualifier-type;
  181. method message() {
  182. "Cannot dispatch to method $.method on {$.qualifier-type.^name} "
  183. ~ "because it is not inherited or done by {$.invocant.^name}";
  184. }
  185. }
  186. my class X::Role::Parametric::NoSuchCandidate is Exception {
  187. has Mu $.role;
  188. method message {
  189. "No appropriate parametric role variant available for '"
  190. ~ $.role.^name
  191. ~ "'";
  192. }
  193. }
  194. my class X::Pragma::NoArgs is Exception {
  195. has $.name;
  196. method message { "The '$.name' pragma does not take any arguments." }
  197. }
  198. my class X::Pragma::CannotPrecomp is Exception {
  199. has $.what = 'This compilation unit';
  200. method message { "$.what may not be pre-compiled" }
  201. }
  202. my class X::Pragma::CannotWhat is Exception {
  203. has $.what;
  204. has $.name;
  205. method message { "'$.what $.name' is not an option." }
  206. }
  207. my class X::Pragma::MustOneOf is Exception {
  208. has $.name;
  209. has $.alternatives;
  210. method message { "'$.name' pragma expects one parameter out of $.alternatives." }
  211. }
  212. my class X::Pragma::UnknownArg is Exception {
  213. has $.name;
  214. has $.arg;
  215. method message { "Unknown argument '{$.arg.perl}' specified with the '$.name' pragma." }
  216. }
  217. my class X::Pragma::OnlyOne is Exception {
  218. has $.name;
  219. method message { "The '$.name' pragma only takes one argument." }
  220. }
  221. my role X::Control is Exception {
  222. }
  223. my class CX::Next does X::Control {
  224. method message() { "<next control exception>" }
  225. }
  226. my class CX::Redo does X::Control {
  227. method message() { "<redo control exception>" }
  228. }
  229. my class CX::Last does X::Control {
  230. method message() { "<last control exception>" }
  231. }
  232. my class CX::Take does X::Control {
  233. method message() { "<take control exception>" }
  234. }
  235. my class CX::Warn does X::Control {
  236. has $.message;
  237. }
  238. my class CX::Succeed does X::Control {
  239. method message() { "<succeed control exception>" }
  240. }
  241. my class CX::Proceed does X::Control {
  242. method message() { "<proceed control exception>" }
  243. }
  244. my class CX::Return does X::Control {
  245. method message() { "<return control exception>" }
  246. }
  247. my class CX::Emit does X::Control {
  248. method message() { "<emit control exception>" }
  249. }
  250. my class CX::Done does X::Control {
  251. method message() { "<done control exception>" }
  252. }
  253. sub EXCEPTION(|) {
  254. my Mu $vm_ex := nqp::shift(nqp::p6argvmarray());
  255. my Mu $payload := nqp::getpayload($vm_ex);
  256. if nqp::p6bool(nqp::istype($payload, Exception)) {
  257. nqp::bindattr($payload, Exception, '$!ex', $vm_ex);
  258. $payload;
  259. } else {
  260. my int $type = nqp::getextype($vm_ex);
  261. my $ex;
  262. if $type +& nqp::const::CONTROL_NEXT {
  263. $ex := CX::Next.new();
  264. }
  265. elsif $type +& nqp::const::CONTROL_REDO {
  266. $ex := CX::Redo.new();
  267. }
  268. elsif $type +& nqp::const::CONTROL_LAST {
  269. $ex := CX::Last.new();
  270. }
  271. elsif $type == nqp::const::CONTROL_TAKE {
  272. $ex := CX::Take.new();
  273. }
  274. elsif $type == nqp::const::CONTROL_WARN {
  275. my str $message = nqp::getmessage($vm_ex);
  276. $message = 'Warning' if nqp::isnull_s($message) || $message eq '';
  277. $ex := CX::Warn.new(:$message);
  278. }
  279. elsif $type == nqp::const::CONTROL_SUCCEED {
  280. $ex := CX::Succeed.new();
  281. }
  282. elsif $type == nqp::const::CONTROL_PROCEED {
  283. $ex := CX::Proceed.new();
  284. }
  285. elsif $type == nqp::const::CONTROL_RETURN {
  286. $ex := CX::Return.new();
  287. }
  288. elsif $type == nqp::const::CONTROL_EMIT {
  289. $ex := CX::Emit.new();
  290. }
  291. elsif $type == nqp::const::CONTROL_DONE {
  292. $ex := CX::Done.new();
  293. }
  294. else {
  295. $ex := nqp::create(X::AdHoc);
  296. nqp::bindattr($ex, X::AdHoc, '$!payload', nqp::p6box_s(nqp::getmessage($vm_ex) // 'unknown exception'));
  297. }
  298. nqp::bindattr($ex, Exception, '$!ex', $vm_ex);
  299. $ex;
  300. }
  301. }
  302. my class X::Comp::AdHoc { ... }
  303. sub COMP_EXCEPTION(|) {
  304. my Mu $vm_ex := nqp::shift(nqp::p6argvmarray());
  305. my Mu $payload := nqp::getpayload($vm_ex);
  306. if nqp::p6bool(nqp::istype($payload, Exception)) {
  307. nqp::bindattr($payload, Exception, '$!ex', $vm_ex);
  308. $payload;
  309. } else {
  310. my $ex := nqp::create(X::Comp::AdHoc);
  311. nqp::bindattr($ex, Exception, '$!ex', $vm_ex);
  312. nqp::bindattr($ex, X::AdHoc, '$!payload', nqp::p6box_s(nqp::getmessage($vm_ex)));
  313. $ex;
  314. }
  315. }
  316. do {
  317. sub print_exception(|) {
  318. my Mu $ex := nqp::atpos(nqp::p6argvmarray(), 0);
  319. my $e := EXCEPTION($ex);
  320. if %*ENV<RAKUDO_EXCEPTIONS_HANDLER> -> $handler {
  321. my $class := ::("Exceptions::$handler");
  322. unless nqp::istype($class,Failure) {
  323. temp %*ENV<RAKUDO_EXCEPTIONS_HANDLER> = ""; # prevent looping
  324. return unless $class.process($e)
  325. }
  326. }
  327. try {
  328. my $v := $e.vault-backtrace;
  329. my Mu $err := $*ERR;
  330. $e.backtrace; # This is where most backtraces actually happen
  331. if $e.is-compile-time || $e.backtrace && $e.backtrace.is-runtime {
  332. $err.say($e.gist);
  333. if $v and !$e.gist.ends-with($v.Str) {
  334. $err.say("Actually thrown at:");
  335. $err.say($v.Str);
  336. }
  337. }
  338. elsif Rakudo::Internals.VERBATIM-EXCEPTION(0) {
  339. $err.print($e.Str);
  340. }
  341. else {
  342. $err.say("===SORRY!===");
  343. $err.say($e.Str);
  344. }
  345. nqp::getcurhllsym('&THE_END')();
  346. CONTROL { when CX::Warn { .resume } }
  347. }
  348. if $! {
  349. nqp::rethrow(nqp::getattr(nqp::decont($!), Exception, '$!ex'));
  350. $ex
  351. }
  352. }
  353. sub print_control(|) {
  354. nqp::stmts(
  355. (my Mu $ex := nqp::atpos(nqp::p6argvmarray(),0)),
  356. (my int $type = nqp::getextype($ex)),
  357. (my $backtrace = Backtrace.new(nqp::backtrace($ex))),
  358. nqp::if(
  359. nqp::iseq_i($type,nqp::const::CONTROL_WARN),
  360. nqp::stmts(
  361. (my Mu $err := $*ERR),
  362. (my str $msg = nqp::getmessage($ex)),
  363. $err.say(nqp::if(nqp::chars($msg),$msg,"Warning")),
  364. $err.print($backtrace.first-none-setting-line),
  365. nqp::resume($ex)
  366. )
  367. )
  368. );
  369. my $label = $type +& nqp::const::CONTROL_LABELED ?? "labeled " !! "";
  370. if $type +& nqp::const::CONTROL_LAST {
  371. X::ControlFlow.new(illegal => "{$label}last", enclosing => 'loop construct', :$backtrace).throw;
  372. }
  373. elsif $type +& nqp::const::CONTROL_NEXT {
  374. X::ControlFlow.new(illegal => "{$label}next", enclosing => 'loop construct', :$backtrace).throw;
  375. }
  376. elsif $type +& nqp::const::CONTROL_REDO {
  377. X::ControlFlow.new(illegal => "{$label}redo", enclosing => 'loop construct', :$backtrace).throw;
  378. }
  379. elsif $type +& nqp::const::CONTROL_PROCEED {
  380. X::ControlFlow.new(illegal => 'proceed', enclosing => 'when clause', :$backtrace).throw;
  381. }
  382. elsif $type +& nqp::const::CONTROL_SUCCEED {
  383. # XXX: should work like leave() ?
  384. X::ControlFlow.new(illegal => 'succeed', enclosing => 'when clause', :$backtrace).throw;
  385. }
  386. elsif $type +& nqp::const::CONTROL_TAKE {
  387. X::ControlFlow.new(illegal => 'take', enclosing => 'gather', :$backtrace).throw;
  388. }
  389. elsif $type +& nqp::const::CONTROL_EMIT {
  390. X::ControlFlow.new(illegal => 'emit', enclosing => 'supply or react', :$backtrace).throw;
  391. }
  392. elsif $type +& nqp::const::CONTROL_DONE {
  393. X::ControlFlow.new(illegal => 'done', enclosing => 'supply or react', :$backtrace).throw;
  394. }
  395. else {
  396. X::ControlFlow.new(illegal => 'control exception', enclosing => 'handler', :$backtrace).throw;
  397. }
  398. }
  399. my Mu $comp := nqp::getcomp('perl6');
  400. $comp.^add_method('handle-exception',
  401. method (|) {
  402. my Mu $ex := nqp::atpos(nqp::p6argvmarray(), 1);
  403. print_exception($ex);
  404. nqp::exit(1);
  405. 0;
  406. }
  407. );
  408. $comp.^add_method('handle-control',
  409. method (|) {
  410. my Mu $ex := nqp::atpos(nqp::p6argvmarray(), 1);
  411. print_control($ex);
  412. nqp::rethrow($ex);
  413. }
  414. );
  415. }
  416. my role X::OS is Exception {
  417. has $.os-error;
  418. method message() { $.os-error }
  419. }
  420. my role X::IO does X::OS { };
  421. my class X::IO::Unknown does X::IO {
  422. has $.trying;
  423. method message { "Unknown IO error trying '$.trying'" }
  424. }
  425. my class X::IO::Rename does X::IO {
  426. has $.from;
  427. has $.to;
  428. method message() {
  429. "Failed to rename '$.from' to '$.to': $.os-error"
  430. }
  431. }
  432. my class X::IO::Copy does X::IO {
  433. has $.from;
  434. has $.to;
  435. method message() {
  436. "Failed to copy '$.from' to '$.to': $.os-error"
  437. }
  438. }
  439. my class X::IO::Lock does X::IO {
  440. has $.lock-type;
  441. method message() { "Could not obtain $.lock-type lock: $.os-error" }
  442. }
  443. my class X::IO::Move does X::IO {
  444. has $.from;
  445. has $.to;
  446. method message() {
  447. "Failed to move '$.from' to '$.to': $.os-error"
  448. }
  449. }
  450. my class X::IO::DoesNotExist does X::IO {
  451. has $.path;
  452. has $.trying;
  453. method message() {
  454. "Failed to find '$.path' while trying to do '.$.trying'"
  455. }
  456. }
  457. my class X::IO::NotAFile does X::IO {
  458. has $.path;
  459. has $.trying;
  460. method message() {
  461. "'$.path' is not a regular file while trying to do '.$.trying'"
  462. }
  463. }
  464. my class X::IO::Null does X::IO {
  465. method message() {
  466. "Cannot use null character (U+0000) as part of the path"
  467. }
  468. }
  469. my class X::IO::Directory does X::IO {
  470. has $.path;
  471. has $.trying;
  472. has $.use;
  473. method message () {
  474. my $x = "'$.path' is a directory, cannot do '.$.trying' on a directory";
  475. if $.use { $x ~= ", try '{$.use}()' instead" }
  476. $x;
  477. }
  478. }
  479. my class X::IO::Symlink does X::IO {
  480. has $.target;
  481. has $.name;
  482. method message() {
  483. "Failed to create symlink called '$.name' on target '$.target': $.os-error"
  484. }
  485. }
  486. my class X::IO::Link does X::IO {
  487. has $.target;
  488. has $.name;
  489. method message() {
  490. "Failed to create link called '$.name' on target '$.target': $.os-error"
  491. }
  492. }
  493. my class X::IO::Mkdir does X::IO {
  494. has $.path;
  495. has $.mode;
  496. method message() {
  497. "Failed to create directory '$.path' with mode '0o{$.mode.fmt("%03o")}': $.os-error"
  498. }
  499. }
  500. my class X::IO::Chdir does X::IO {
  501. has $.path;
  502. method message() {
  503. "Failed to change the working directory to '$.path': $.os-error"
  504. }
  505. }
  506. my class X::IO::Dir does X::IO {
  507. has $.path;
  508. method message() {
  509. "Failed to get the directory contents of '$.path': $.os-error"
  510. }
  511. }
  512. my class X::IO::Cwd does X::IO {
  513. method message() {
  514. "Failed to get the working directory: $.os-error"
  515. }
  516. }
  517. my class X::IO::Flush does X::IO {
  518. method message() {
  519. "Cannot flush handle: $.os-error"
  520. }
  521. }
  522. my class X::IO::NotAChild does X::IO {
  523. has $.path;
  524. has $.child;
  525. method message() {
  526. "Path {$.child.perl} is not a child of path {$.path.perl}"
  527. }
  528. }
  529. my class X::IO::Resolve does X::IO {
  530. has $.path;
  531. method message() { "Failed to completely resolve {$.path.perl}" }
  532. }
  533. my class X::IO::Rmdir does X::IO {
  534. has $.path;
  535. method message() {
  536. "Failed to remove the directory '$.path': $.os-error"
  537. }
  538. }
  539. my class X::IO::Unlink does X::IO {
  540. has $.path;
  541. method message() {
  542. "Failed to remove the file '$.path': $.os-error"
  543. }
  544. }
  545. my class X::IO::Chmod does X::IO {
  546. has $.path;
  547. has $.mode;
  548. method message() {
  549. "Failed to set the mode of '$.path' to '0o{$.mode.fmt("%03o")}': $.os-error"
  550. }
  551. }
  552. my class X::IO::BinaryAndEncoding does X::IO {
  553. method message { "Cannot open a handle in binary mode (:bin) and also specify an encoding" }
  554. }
  555. my class X::IO::BinaryMode does X::IO {
  556. has $.trying;
  557. method message { "Cannot do '$.trying' on a handle in binary mode" }
  558. }
  559. my role X::Comp is Exception {
  560. has $.filename;
  561. has $.pos;
  562. has $.line;
  563. has $.column;
  564. has @.modules;
  565. has $.is-compile-time = False;
  566. has $.pre;
  567. has $.post;
  568. has @.highexpect;
  569. multi method gist(::?CLASS:D: :$sorry = True, :$expect = True) {
  570. if $.is-compile-time {
  571. my ($red,$clear,$green,$yellow,$eject) =
  572. Rakudo::Internals.error-rcgye;
  573. my $r = $sorry ?? self.sorry_heading() !! "";
  574. $r ~= $.filename eq '<unknown file>'
  575. ?? $.line == 1
  576. ?? $.message
  577. !! "$.message\nat line $.line"
  578. !! "$.message\nat $.filename():$.line";
  579. $r ~= "\n------> $green$.pre$yellow$eject$red$.post$clear" if defined $.pre;
  580. if $expect && @.highexpect {
  581. $r ~= "\n expecting any of:";
  582. for flat @.highexpect».list {
  583. $r ~= "\n $_";
  584. }
  585. }
  586. for @.modules.reverse[1..*] {
  587. my $line = nqp::p6box_i($_<line>);
  588. $r ~= $_<module>.defined
  589. ?? "\n from module $_<module> ($_<filename> line $line)"
  590. !! "\n from $_<filename> line $line";
  591. }
  592. $r;
  593. }
  594. else {
  595. self.Exception::gist;
  596. }
  597. }
  598. method sorry_heading() {
  599. my ($red, $clear) = Rakudo::Internals.error-rcgye;
  600. "$red==={$clear}SORRY!$red===$clear Error while compiling{
  601. $.filename eq '<unknown file>'
  602. ?? ':'
  603. !! " $.filename"
  604. }\n"
  605. }
  606. method SET_FILE_LINE($file, $line) {
  607. $!filename = $file;
  608. $!line = $line;
  609. $!is-compile-time = True;
  610. }
  611. }
  612. my class X::Comp::Group is Exception {
  613. has $.panic;
  614. has @.sorrows;
  615. has @.worries;
  616. method is-compile-time(--> True) { }
  617. multi method gist(::?CLASS:D:) {
  618. my $r = "";
  619. if $.panic || @.sorrows {
  620. my ($red, $clear) = Rakudo::Internals.error-rcgye;
  621. $r ~= "$red==={$clear}SORRY!$red===$clear\n";
  622. for @.sorrows {
  623. $r ~= .gist(:!sorry, :!expect) ~ "\n";
  624. }
  625. if $.panic {
  626. $r ~= $.panic.gist(:!sorry) ~ "\n";
  627. }
  628. }
  629. if @.worries {
  630. $r ~= $.panic || @.sorrows
  631. ?? "Other potential difficulties:\n"
  632. !! "Potential difficulties:\n";
  633. for @.worries {
  634. $r ~= .gist(:!sorry, :!expect).indent(4) ~ "\n";
  635. }
  636. }
  637. $r
  638. }
  639. method message() {
  640. my @m;
  641. for @.sorrows {
  642. @m.append(.message);
  643. }
  644. if $.panic {
  645. @m.append($.panic.message);
  646. }
  647. for @.worries {
  648. @m.append(.message);
  649. }
  650. @m.join("\n")
  651. }
  652. }
  653. my role X::MOP is Exception { }
  654. my class X::Comp::BeginTime does X::Comp {
  655. has str $.use-case;
  656. has $.exception;
  657. method message() {
  658. $!exception ~~ X::MOP
  659. ?? $!exception.message
  660. !! "An exception occurred while $!use-case"
  661. }
  662. multi method gist(::?CLASS:D: :$sorry = True) {
  663. my $r = $sorry ?? self.sorry_heading() !! "";
  664. $r ~= "$.message\nat $.filename():$.line";
  665. for @.modules.reverse[1..*] {
  666. my $line = nqp::p6box_i($_<line>);
  667. $r ~= $_<module>.defined
  668. ?? "\n from module $_<module> ($_<filename> line $line)"
  669. !! "\n from $_<filename> line $line";
  670. }
  671. unless $!exception ~~ X::MOP {
  672. $r ~= "\nException details:\n" ~ $!exception.gist.indent(2);
  673. }
  674. $r;
  675. }
  676. }
  677. # XXX a hack for getting line numbers from exceptions from the metamodel
  678. my class X::Comp::AdHoc is X::AdHoc does X::Comp {
  679. method is-compile-time(--> True) { }
  680. }
  681. my class X::Comp::FailGoal does X::Comp {
  682. has $.dba;
  683. has $.goal;
  684. has $.line-real;
  685. method is-compile-time(--> True) { }
  686. method message { "Unable to parse expression in $.dba; couldn't find final $.goal"
  687. ~ " (corresponding starter was at line $.line-real)" }
  688. }
  689. my role X::Syntax does X::Comp { }
  690. my role X::Pod { }
  691. my class X::NYI is Exception {
  692. has $.feature;
  693. has $.did-you-mean;
  694. has $.workaround;
  695. method message() {
  696. my $msg = "$.feature not yet implemented. Sorry.";
  697. $msg ~= "\nDid you mean: {$.did-you-mean.gist}?" if $.did-you-mean;
  698. $msg ~= "\nWorkaround: $.workaround" if $.workaround;
  699. $msg
  700. }
  701. }
  702. my class X::Comp::NYI is X::NYI does X::Comp { };
  703. my class X::NYI::Available is X::NYI {
  704. has @.available = die("Must give :available<modules> for installation. ");
  705. method available-str {
  706. my @a = @.available;
  707. my $a = @a.pop;
  708. @a ?? (@a.join(', ') || (), $a).join(" or ") !! $a;
  709. }
  710. method message() {
  711. "Please install { self.available-str } for $.feature support. "
  712. }
  713. }
  714. my class X::NYI::BigInt is Exception {
  715. has $.op;
  716. has $.big;
  717. has $.side = 'right';
  718. method message() {
  719. "Big integer $!big not yet supported on {$!side}hand side of '$!op' operator"
  720. }
  721. }
  722. my class X::Experimental does X::Comp {
  723. has $.feature;
  724. has $.use = $!feature;
  725. method message() { "Use of $.feature is experimental; please 'use experimental :$.use'" }
  726. }
  727. my class X::Worry is Exception { }
  728. my class X::Worry::P5 is X::Worry { }
  729. my class X::Worry::P5::Reference is X::Worry::P5 {
  730. method message {
  731. q/To pass an array, hash or sub to a function in Perl 6, just pass it as is.
  732. For other uses of Perl 5's ref operator consider binding with ::= instead.
  733. Parenthesize as \\(...) if you intended a capture of a single variable./
  734. }
  735. }
  736. my class X::Worry::P5::BackReference is X::Worry::P5 {
  737. method message {
  738. q/To refer to a positional match capture, just use $0 (numbering starts at 0).
  739. Parenthesize as \\(...) if you intended a capture of a single numeric value./
  740. }
  741. }
  742. my class X::Worry::P5::LeadingZero is X::Worry::P5 {
  743. has $.value;
  744. method message {
  745. 'Leading 0 has no meaning. If you meant to create an octal number'
  746. ~ ", use '0o' prefix" ~ (
  747. $!value.comb.grep(*.unival > 7)
  748. ?? ", but note that $!value is not a valid octal number"
  749. !! "; like, '0o$!value'"
  750. ) ~ '. If you meant to create a string, please add quotation marks.'
  751. }
  752. }
  753. my class X::Worry::Precedence::Range is X::Worry {
  754. has $.action;
  755. method message {
  756. "To $!action a range, parenthesize the whole range.
  757. (Or parenthesize the whole endpoint expression, if you meant that.)"
  758. }
  759. }
  760. my class X::Trait::Invalid is Exception {
  761. has $.type; # is, will, of etc.
  762. has $.subtype; # wrong subtype being tried
  763. has $.declaring; # variable, sub, parameter, etc.
  764. has $.name; # '$foo', '@bar', etc.
  765. method message () {
  766. "Cannot use '$.type $.subtype' on $.declaring '$.name'."
  767. }
  768. }
  769. my class X::Trait::Unknown is Exception {
  770. has $.type; # is, will, of etc.
  771. has $.subtype; # wrong subtype being tried
  772. has $.declaring; # variable, sub, parameter, etc.
  773. method message () {
  774. "Can't use unknown trait '$.type $.subtype' in a$.declaring declaration."
  775. }
  776. }
  777. my class X::Comp::Trait::Unknown is X::Trait::Unknown does X::Comp { };
  778. my class X::Trait::NotOnNative is Exception {
  779. has $.type; # is, will, of etc.
  780. has $.subtype; # wrong subtype being tried
  781. has $.native; # type of native (optional)
  782. method message () {
  783. "Can't use trait '$.type $.subtype' on a native"
  784. ~ ( $.native ?? " $.native." !! "." );
  785. }
  786. }
  787. my class X::Comp::Trait::NotOnNative is X::Trait::NotOnNative does X::Comp { };
  788. my class X::Trait::Scope is Exception {
  789. has $.type; # is, will, of etc.
  790. has $.subtype; # export
  791. has $.declaring; # type name of the object
  792. has $.scope; # not supported (but used) scope
  793. has $.supported; # hint about what is allowed instead
  794. method message () {
  795. "Can't apply trait '$.type $.subtype' on a $.scope scoped $.declaring."
  796. ~ ( $.supported ?? " Only {$.supported.join(' and ')} scoped {$.declaring}s are supported." !! '' );
  797. }
  798. }
  799. my class X::Comp::Trait::Scope is X::Trait::Scope does X::Comp { };
  800. my class X::Exhausted is Exception {
  801. has $.what;
  802. has $.reason;
  803. method message {
  804. $.reason
  805. ?? "Could not create another $.what because of: $.reason"
  806. !! "Could not create another $.what"
  807. }
  808. }
  809. my class X::OutOfRange is Exception {
  810. has $.what = 'Argument';
  811. has $.got = '<unknown>';
  812. has $.range = '<unknown>';
  813. has $.comment;
  814. method message() {
  815. my $result = $.comment.defined
  816. ?? "$.what out of range. Is: $.got, should be in $.range.gist(); $.comment"
  817. !! "$.what out of range. Is: $.got, should be in $.range.gist()";
  818. $result;
  819. }
  820. }
  821. my class X::Buf::AsStr is Exception {
  822. has $.method;
  823. method message() {
  824. "Cannot use a Buf as a string, but you called the $.method method on it";
  825. }
  826. }
  827. my class X::Buf::Pack is Exception {
  828. has $.directive;
  829. method message() {
  830. "Unrecognized directive '$.directive'";
  831. }
  832. }
  833. my class X::Buf::Pack::NonASCII is Exception {
  834. has $.char;
  835. method message() {
  836. "non-ASCII character '$.char' while processing an 'A' template in pack";
  837. }
  838. }
  839. my class X::Signature::Placeholder does X::Comp {
  840. has $.placeholder;
  841. method message() {
  842. "Placeholder variable '$.placeholder' cannot override existing signature";
  843. }
  844. }
  845. my class X::Placeholder::Block does X::Comp {
  846. has $.placeholder;
  847. method message() {
  848. "Placeholder variable $.placeholder may not be used here because the surrounding block takes no signature";
  849. }
  850. }
  851. my class X::Placeholder::NonPlaceholder does X::Comp {
  852. has $.variable_name;
  853. has $.placeholder;
  854. has $.decl;
  855. method message() {
  856. my $decl = $!decl ?? ' ' ~ $!decl !! '';
  857. "$!variable_name has already been used as a non-placeholder in the surrounding$decl block,\n" ~
  858. " so you will confuse the reader if you suddenly declare $!placeholder here"
  859. }
  860. }
  861. my class X::Placeholder::Mainline is X::Placeholder::Block {
  862. method message() {
  863. "Cannot use placeholder parameter $.placeholder outside of a sub or block"
  864. }
  865. }
  866. my class X::Placeholder::Attribute is X::Placeholder::Block {
  867. method message() {
  868. "Cannot use placeholder parameter $.placeholder in an attribute initializer"
  869. }
  870. }
  871. my class X::Undeclared does X::Comp {
  872. has $.what = 'Variable';
  873. has $.symbol;
  874. has @.suggestions;
  875. method message() {
  876. my $message := "$.what '$.symbol' is not declared";
  877. if +@.suggestions == 1 {
  878. $message := "$message. Did you mean '@.suggestions[0]'?";
  879. } elsif +@.suggestions > 1 {
  880. $message := "$message. Did you mean any of these?\n { @.suggestions.join("\n ") }\n";
  881. }
  882. $message;
  883. }
  884. }
  885. my class X::Attribute::Undeclared is X::Undeclared {
  886. has $.package-kind;
  887. has $.package-name;
  888. method message() {
  889. "Attribute $.symbol not declared in $.package-kind $.package-name";
  890. }
  891. }
  892. my class X::Attribute::Regex is X::Undeclared {
  893. method message() {
  894. "Attribute $.symbol not available inside of a regex, since regexes are methods on Cursor.\n" ~
  895. "Consider storing the attribute in a lexical, and using that in the regex.";
  896. }
  897. }
  898. my class X::Undeclared::Symbols does X::Comp {
  899. has %.post_types;
  900. has %.unk_types;
  901. has %.unk_routines;
  902. has %.routine_suggestion;
  903. has %.type_suggestion;
  904. multi method gist(X::Undeclared::Symbols:D: :$sorry = True) {
  905. ($sorry ?? self.sorry_heading() !! "") ~ self.message
  906. }
  907. method message(X::Undeclared::Symbols:D:) {
  908. sub l(@l) {
  909. my @lu = @l.map({ nqp::hllize($_) }).unique.sort;
  910. 'used at line' ~ (@lu == 1 ?? ' ' !! 's ') ~ @lu.join(', ')
  911. }
  912. sub s(@s) {
  913. "Did you mean '{ @s.join("', '") }'?";
  914. }
  915. my $r = "";
  916. if %.post_types {
  917. $r ~= "Illegally post-declared type" ~ (%.post_types.elems == 1 ?? "" !! "s") ~ ":\n";
  918. for %.post_types.sort(*.key) {
  919. $r ~= " $_.key() &l($_.value)\n";
  920. }
  921. }
  922. if %.unk_types {
  923. $r ~= "Undeclared name" ~ (%.unk_types.elems == 1 ?? "" !! "s") ~ ":\n";
  924. for %.unk_types.sort(*.key) {
  925. $r ~= " $_.key() &l($_.value)";
  926. if +%.type_suggestion{$_.key()} {
  927. $r ~= ". " ~ s(%.type_suggestion{$_.key()});
  928. }
  929. $r ~= "\n";
  930. }
  931. }
  932. if %.unk_routines {
  933. my $obs = {
  934. y => "tr",
  935. qr => "rx",
  936. local => "temp (or dynamic var)",
  937. new => "method call syntax",
  938. foreach => "for",
  939. use => '"v" prefix for pragma (e.g., "use v6;", "use v6.c;")',
  940. need => '"v" prefix and "use" for pragma (e.g., "use v6;", "use v6.c;")',
  941. }
  942. $r ~= "Undeclared routine" ~ (%.unk_routines.elems == 1 ?? "" !! "s") ~ ":\n";
  943. for %.unk_routines.sort(*.key) {
  944. $r ~= " $_.key() &l($_.value)";
  945. $r ~= " (in Perl 6 please use " ~ $obs{$_.key()} ~ " instead)" if $obs{$_.key()};
  946. if +%.routine_suggestion{$_.key()}.list {
  947. $r ~= ". " ~ s(%.routine_suggestion{$_.key()}.list);
  948. }
  949. $r ~= "\n";
  950. }
  951. }
  952. $r
  953. }
  954. }
  955. my class X::Redeclaration does X::Comp {
  956. has $.symbol;
  957. has $.postfix = '';
  958. has $.what = 'symbol';
  959. method message() {
  960. "Redeclaration of $.what '$.symbol'"
  961. ~ (" $.postfix" if $.postfix)
  962. ~ (" (did you mean to declare a multi-sub?)" if $.what eq 'routine');
  963. }
  964. }
  965. my class X::Redeclaration::Outer does X::Comp {
  966. has $.symbol;
  967. method message() {
  968. "Lexical symbol '$.symbol' is already bound to an outer symbol;\n" ~
  969. "the implicit outer binding must be rewritten as OUTER::<$.symbol>\n" ~
  970. "before you can unambiguously declare a new '$.symbol' in this scope";
  971. }
  972. }
  973. my class X::Dynamic::Postdeclaration does X::Comp {
  974. has $.symbol;
  975. method message() {
  976. "Illegal post-declaration of dynamic variable '$.symbol';\n" ~
  977. "earlier access must be written as CALLERS::<$.symbol>\n" ~
  978. "if that's what you meant"
  979. }
  980. }
  981. my class X::Dynamic::Package does X::Comp {
  982. has $.symbol;
  983. method message() {
  984. "Dynamic variables cannot have package-like names, like $!symbol"
  985. }
  986. }
  987. my class X::Import::Redeclaration does X::Comp {
  988. has @.symbols;
  989. has $.source-package-name;
  990. method message() {
  991. @.symbols == 1
  992. ?? "Cannot import symbol @.symbols[0] from $.source-package-name, because it already exists in this lexical scope"
  993. !! ("Cannot import the following symbols from $.source-package-name, because they already exist in this lexical scope: ", @.symbols.join(', '));
  994. }
  995. }
  996. my class X::Import::OnlystarProto does X::Comp {
  997. has @.symbols;
  998. has $.source-package-name;
  999. method message() {
  1000. @.symbols == 1
  1001. ?? "Cannot import symbol @.symbols[0] from $.source-package-name, only onlystar-protos can be merged"
  1002. !! ("Cannot import the following symbols from $.source-package-name, only onlystar-protos can be merged: ", @.symbols.join(', '));
  1003. }
  1004. }
  1005. my class X::PoisonedAlias does X::Comp {
  1006. has $.alias;
  1007. has $.package-type = 'package';
  1008. has $.package-name;
  1009. method message() {
  1010. "Cannot use poisoned alias $!alias, because it was declared by several {$!package-type}s." ~
  1011. ($!package-name ?? "\nPlease access it via explicit package name like: {$!package-name}::{$!alias}" !! '')
  1012. }
  1013. }
  1014. my class X::Phaser::Multiple does X::Comp {
  1015. has $.block;
  1016. method message() { "Only one $.block block is allowed" }
  1017. }
  1018. my class X::Obsolete does X::Comp {
  1019. has $.old;
  1020. has $.replacement; # can't call it $.new, collides with constructor
  1021. has $.when = 'in Perl 6';
  1022. method message() { "Unsupported use of $.old; $.when please use $.replacement" }
  1023. }
  1024. my class X::Parameter::Default does X::Comp {
  1025. has $.how;
  1026. has $.parameter;
  1027. method message() {
  1028. $.parameter
  1029. ?? "Cannot put default on $.how parameter $.parameter"
  1030. !! "Cannot put default on anonymous $.how parameter";
  1031. }
  1032. }
  1033. my class X::Parameter::Default::TypeCheck does X::Comp {
  1034. has $.got is default(Nil);
  1035. has $.expected is default(Nil);
  1036. method message() {
  1037. "Default value '$.got.gist()' will never bind to a parameter of type $.expected.^name()"
  1038. }
  1039. }
  1040. my class X::Parameter::AfterDefault does X::Syntax {
  1041. has $.type;
  1042. has $.modifier;
  1043. has $.default;
  1044. method message() {
  1045. "The $.type \"$.modifier\" came after the default value\n"
  1046. ~ "(did you mean \"...$.modifier $.default\"?)"
  1047. }
  1048. }
  1049. my class X::Parameter::Placeholder does X::Comp {
  1050. has $.parameter;
  1051. has $.right;
  1052. method message() {
  1053. "In signature parameter, placeholder variables like $.parameter are illegal\n"
  1054. ~ "you probably meant a named parameter: '$.right'";
  1055. }
  1056. }
  1057. my class X::Parameter::Twigil does X::Comp {
  1058. has $.parameter;
  1059. has $.twigil;
  1060. method message() {
  1061. "In signature parameter $.parameter, it is illegal to use the $.twigil twigil";
  1062. }
  1063. }
  1064. my class X::Parameter::MultipleTypeConstraints does X::Comp {
  1065. has $.parameter;
  1066. method message() {
  1067. ($.parameter ?? "Parameter $.parameter" !! 'A parameter')
  1068. ~ " may only have one prefix type constraint";
  1069. }
  1070. }
  1071. my class X::Parameter::BadType does X::Comp {
  1072. has Mu $.type;
  1073. method message() {
  1074. my $what = ~$!type.HOW.WHAT.^name.match(/ .* '::' <(.*)> HOW/) // 'Namespace';
  1075. "$what $!type.^name() is insufficiently type-like to qualify a parameter"
  1076. }
  1077. }
  1078. my class X::Parameter::WrongOrder does X::Comp {
  1079. has $.misplaced;
  1080. has $.parameter;
  1081. has $.after;
  1082. method message() {
  1083. "Cannot put $.misplaced parameter $.parameter after $.after parameters";
  1084. }
  1085. }
  1086. my class X::Parameter::InvalidConcreteness is Exception {
  1087. has $.expected;
  1088. has $.got;
  1089. has $.routine;
  1090. has $.param;
  1091. has Bool $.should-be-concrete;
  1092. has Bool $.param-is-invocant;
  1093. method message() {
  1094. $!routine = '<anon>' if not $!routine.defined or $!routine eq '';
  1095. $!param = '<anon>' if not $!param.defined or $!param eq '';
  1096. my $beginning = $!param-is-invocant ?? 'Invocant of method' !! "Parameter '$!param' of routine";
  1097. my $must-be = $!should-be-concrete ?? 'an object instance' !! 'a type object';
  1098. my $not-a = $!should-be-concrete ?? 'a type object' !! 'an object instance';
  1099. my $suggestion = $!should-be-concrete ?? '.new' !! 'multi';
  1100. "$beginning '$!routine' must be $must-be of type '$!expected', not $not-a of type '$!got'. Did you forget a '$suggestion'?"
  1101. }
  1102. }
  1103. my class X::Parameter::InvalidType does X::Comp {
  1104. has $.typename;
  1105. has @.suggestions;
  1106. method message() {
  1107. my $msg := "Invalid typename '$.typename' in parameter declaration.";
  1108. if +@.suggestions > 0 {
  1109. $msg := $msg ~ " Did you mean '" ~ @.suggestions.join("', '") ~ "'?";
  1110. }
  1111. $msg;
  1112. }
  1113. }
  1114. my class X::Parameter::RW is Exception {
  1115. has $.got;
  1116. has $.symbol;
  1117. method message() {
  1118. "Parameter '$.symbol' expected a writable container, but got $.got.^name() value"
  1119. }
  1120. }
  1121. my class X::Parameter::TypedSlurpy does X::Comp {
  1122. has $.kind;
  1123. method message() {
  1124. "Slurpy $.kind parameters with type constraints are not supported"
  1125. }
  1126. }
  1127. my class X::Signature::NameClash does X::Comp {
  1128. has $.name;
  1129. method message() {
  1130. "Name $.name used for more than one named parameter";
  1131. }
  1132. }
  1133. my class X::Method::Private::Permission does X::Comp {
  1134. has $.method;
  1135. has $.source-package;
  1136. has $.calling-package;
  1137. method message() {
  1138. "Cannot call private method '$.method' on package $.source-package because it does not trust $.calling-package";
  1139. }
  1140. }
  1141. my class X::Method::Private::Unqualified does X::Comp {
  1142. has $.method;
  1143. method message() {
  1144. "Private method call to $.method must be fully qualified with the package containing the method";
  1145. }
  1146. }
  1147. my class X::Adverb is Exception {
  1148. has $.what;
  1149. has $.source;
  1150. has @.unexpected;
  1151. has @.nogo;
  1152. method message {
  1153. my $text = '';
  1154. if @!unexpected.elems -> $elems {
  1155. $text = $elems > 1
  1156. ?? "$elems unexpected adverbs (@.unexpected[])"
  1157. !! "Unexpected adverb '@!unexpected[0]'"
  1158. }
  1159. if @!nogo {
  1160. $text ~= $text ?? " and u" !! "U";
  1161. $text ~= "nsupported combination of adverbs (@.nogo[])";
  1162. }
  1163. $text ~ " passed to $!what on $!source";
  1164. }
  1165. method unexpected { @!unexpected.sort }
  1166. method nogo { @!nogo.sort }
  1167. }
  1168. my class X::Bind is Exception {
  1169. has $.target;
  1170. method message() {
  1171. $.target.defined
  1172. ?? "Cannot bind to $.target"
  1173. !! 'Cannot use bind operator with this left-hand side'
  1174. }
  1175. }
  1176. my class X::Bind::NativeType does X::Comp {
  1177. has $.name;
  1178. method message() {
  1179. "Cannot bind to natively typed variable '$.name'; use assignment instead"
  1180. }
  1181. }
  1182. my class X::Bind::Slice is Exception {
  1183. has $.type;
  1184. method message() {
  1185. "Cannot bind to {$.type.^name} slice";
  1186. }
  1187. }
  1188. my class X::Bind::ZenSlice is X::Bind::Slice {
  1189. method message() {
  1190. "Cannot bind to {$.type.^name} zen slice";
  1191. }
  1192. }
  1193. my class X::Subscript::Negative is Exception {
  1194. has $.index;
  1195. has $.type;
  1196. method message() {
  1197. "Calculated index ({$.index}) is negative, but {$.type.^name} allows only 0-based indexing";
  1198. }
  1199. }
  1200. my class X::Invalid::Value is Exception {
  1201. has $.method;
  1202. has $.name;
  1203. has $.value;
  1204. method message {
  1205. "Invalid value '$.value' for :$.name on method $.method"
  1206. }
  1207. }
  1208. my class X::Invalid::ComputedValue is Exception {
  1209. has $.method;
  1210. has $.name;
  1211. has $.value;
  1212. has $.reason;
  1213. method message {
  1214. "$.name {"on $.method " if $.method}computed to $.value,"
  1215. ~ " which cannot be used"
  1216. ~ (" because $.reason" if $.reason);
  1217. }
  1218. }
  1219. my class X::Value::Dynamic does X::Comp {
  1220. has $.what;
  1221. method message() { "$.what value must be known at compile time" }
  1222. }
  1223. my class X::Syntax::Name::Null does X::Syntax {
  1224. method message() { 'Name component may not be null'; }
  1225. }
  1226. my class X::Syntax::UnlessElse does X::Syntax {
  1227. has $.keyword;
  1228. method message() { qq|"unless" does not take "$!keyword", please rewrite using "if"| }
  1229. }
  1230. my class X::Syntax::WithoutElse does X::Syntax {
  1231. has $.keyword;
  1232. method message() { qq|"without" does not take "$!keyword", please rewrite using "with"| }
  1233. }
  1234. my class X::Syntax::KeywordAsFunction does X::Syntax {
  1235. has $.word;
  1236. has $.needparens;
  1237. method message {
  1238. "Word '$.word' interpreted as '{$.word}()' function call; please use whitespace "
  1239. ~ ($.needparens ?? 'around the parens' !! 'instead of parens')
  1240. }
  1241. }
  1242. my class X::Syntax::Malformed::Elsif does X::Syntax {
  1243. has $.what = 'else if';
  1244. method message() { qq{In Perl 6, please use "elsif' instead of "$.what"} }
  1245. }
  1246. my class X::Syntax::Reserved does X::Syntax {
  1247. has $.reserved;
  1248. has $.instead = '';
  1249. method message() { "The $.reserved is reserved$.instead" }
  1250. }
  1251. my class X::Syntax::P5 does X::Syntax {
  1252. method message() { 'This appears to be Perl 5 code' }
  1253. }
  1254. my class X::Syntax::NegatedPair does X::Syntax {
  1255. has $.key;
  1256. method message() { "Argument not allowed on negated pair with key '$.key'" }
  1257. }
  1258. my class X::Syntax::Variable::Numeric does X::Syntax {
  1259. has $.what = 'variable';
  1260. method message() { "Cannot declare a numeric $.what" }
  1261. }
  1262. my class X::Syntax::Variable::Match does X::Syntax {
  1263. method message() { 'Cannot declare a match variable' }
  1264. }
  1265. my class X::Syntax::Variable::Initializer does X::Syntax {
  1266. has $.name = '<anon>';
  1267. method message() { "Cannot use variable $!name in declaration to initialize itself" }
  1268. }
  1269. my class X::Syntax::Variable::Twigil does X::Syntax {
  1270. has $.what = 'variable';
  1271. has $.twigil;
  1272. has $.scope;
  1273. has $.additional = '';
  1274. method message() { "Cannot use $.twigil twigil on '$.scope' $.what$.additional" }
  1275. }
  1276. my class X::Syntax::Variable::IndirectDeclaration does X::Syntax {
  1277. method message() { 'Cannot declare a variable by indirect name (use a hash instead?)' }
  1278. }
  1279. my class X::Syntax::Variable::BadType does X::Comp {
  1280. has Mu $.type;
  1281. method message() {
  1282. my $what = ~$!type.HOW.WHAT.^name.match(/ .* '::' <(.*)> HOW/) // 'Namespace';
  1283. "$what $!type.^name() is insufficiently type-like to qualify a variable"
  1284. }
  1285. }
  1286. my class X::Syntax::Variable::ConflictingTypes does X::Comp {
  1287. has Mu $.outer;
  1288. has Mu $.inner;
  1289. method message() {
  1290. "$!inner.^name() not allowed here; variable list already declared with type $!outer.^name()"
  1291. }
  1292. }
  1293. my class X::Syntax::Augment::WithoutMonkeyTyping does X::Syntax {
  1294. method message() { "augment not allowed without 'use MONKEY-TYPING'" };
  1295. }
  1296. my class X::Syntax::Augment::Illegal does X::Syntax {
  1297. has $.package;
  1298. method message() { "Cannot augment $.package because it is closed" };
  1299. }
  1300. my class X::Syntax::Augment::Adverb does X::Syntax {
  1301. method message() { "Cannot put adverbs on a typename when augmenting" }
  1302. }
  1303. my class X::Syntax::Type::Adverb does X::Syntax {
  1304. has $.adverb;
  1305. method message() { "Cannot use adverb $.adverb on a type name (only 'ver' and 'auth' are understood)" }
  1306. }
  1307. my class X::Syntax::Argument::MOPMacro does X::Syntax {
  1308. has $.macro;
  1309. method message() { "Cannot give arguments to $.macro" };
  1310. }
  1311. my class X::Role::Initialization is Exception {
  1312. has $.role;
  1313. method message() { "Can only supply an initialization value for a role if it has a single public attribute, but this is not the case for '{$.role.^name}'" }
  1314. }
  1315. my class X::Syntax::Comment::Embedded does X::Syntax {
  1316. method message() { "Opening bracket required for #` comment" }
  1317. }
  1318. my class X::Syntax::Pod::BeginWithoutIdentifier does X::Syntax does X::Pod {
  1319. method message() {
  1320. '=begin must be followed by an identifier; (did you mean "=begin pod"?)'
  1321. }
  1322. }
  1323. my class X::Syntax::Pod::BeginWithoutEnd does X::Syntax does X::Pod {
  1324. has $.type;
  1325. has $.spaces;
  1326. has $.instead;
  1327. method message() {
  1328. if $.instead {
  1329. qq{Expected "=end $.type" to terminate "=begin $.type"; found "=end $.instead" instead.}
  1330. } else {
  1331. "'=begin' not terminated by matching '$.spaces=end $.type'"
  1332. }
  1333. }
  1334. }
  1335. my class X::Syntax::Confused does X::Syntax {
  1336. has $.reason = 'unknown';
  1337. method message() { $.reason eq 'unknown' ?? 'Confused' !! $.reason }
  1338. }
  1339. my class X::Syntax::Malformed does X::Syntax {
  1340. has $.what;
  1341. method message() { "Malformed $.what" }
  1342. }
  1343. my class X::Syntax::Missing does X::Syntax {
  1344. has $.what;
  1345. method message() { "Missing $.what" }
  1346. }
  1347. my class X::Syntax::BlockGobbled does X::Syntax {
  1348. has $.what;
  1349. method message() {
  1350. my $looks_like_type = $.what ~~ /'::' | <[A..Z]><[a..z]>+/;
  1351. $.what ~~ /^'is '/
  1352. ?? "Trait '$.what' needs whitespace before block"
  1353. !! "{ $.what ?? "Function '$.what'" !! 'Expression' } needs parens to avoid gobbling block" ~
  1354. ($looks_like_type ?? " (or perhaps it's a class that's not declared or available in this scope?)" !! "");
  1355. };
  1356. }
  1357. my class X::Syntax::ConditionalOperator::PrecedenceTooLoose does X::Syntax {
  1358. has $.operator;
  1359. method message() { "Precedence of $.operator is too loose to use inside ?? !!; please parenthesize" }
  1360. }
  1361. my class X::Syntax::ConditionalOperator::SecondPartGobbled does X::Syntax {
  1362. method message() { "Your !! was gobbled by the expression in the middle; please parenthesize" }
  1363. }
  1364. my class X::Syntax::ConditionalOperator::SecondPartInvalid does X::Syntax {
  1365. has $.second-part;
  1366. method message() { "Please use !! rather than $.second-part" }
  1367. }
  1368. my class X::Syntax::Perl5Var does X::Syntax {
  1369. has $.name;
  1370. has $.identifier-name;
  1371. my %m =
  1372. '$*' => '^^ and $$',
  1373. '$"' => '.join() method',
  1374. '$$' => '$*PID',
  1375. '$(' => '$*GID',
  1376. '$)' => '$*EGID',
  1377. '$<' => '$*UID',
  1378. '$>' => '$*EUID',
  1379. '$;' => 'real multidimensional hashes',
  1380. '$&' => '$<>',
  1381. '$`' => '$/.prematch',
  1382. '$\'' => '$/.postmatch',
  1383. '$,' => '$*OUT.output_field_separator()',
  1384. '$.' => "the .kv method on e.g. .lines",
  1385. '$/' => "the filehandle's .nl-in attribute",
  1386. '$\\' => "the filehandle's .nl-out attribute",
  1387. '$|' => ':autoflush on open',
  1388. '$?' => '$! for handling child errors also',
  1389. '$@' => '$!',
  1390. '$#' => '.fmt',
  1391. '$[' => 'user-defined array indices',
  1392. '$]' => '$*PERL.version or $*PERL.compiler.version',
  1393. '$^C' => 'COMPILING namespace',
  1394. '$^D' => '$*DEBUGGING',
  1395. '$^E' => '$!.extended_os_error',
  1396. '$^F' => '$*SYSTEM_FD_MAX',
  1397. '$^H' => '$?FOO variables',
  1398. '$^I' => '$*INPLACE',
  1399. '$^M' => 'a global form such as $*M',
  1400. '$^N' => '$/[*-1]',
  1401. '$^O' => 'VM.osname',
  1402. '$^R' => 'an explicit result variable',
  1403. '$^S' => 'context function',
  1404. '$^T' => '$*INIT-INSTANT',
  1405. '$^V' => '$*PERL.version or $*PERL.compiler.version',
  1406. '$^W' => '$*WARNING',
  1407. '$^X' => '$*EXECUTABLE-NAME',
  1408. '$:' => 'Form module',
  1409. '$-' => 'Form module',
  1410. '$+' => 'Form module',
  1411. '$=' => 'Form module',
  1412. '$%' => 'Form module',
  1413. '$^' => 'Form module',
  1414. '$~' => 'Form module',
  1415. '$^A' => 'Form module',
  1416. '$^L' => 'Form module',
  1417. '@-' => '.from method',
  1418. '@+' => '.to method',
  1419. '%-' => '.from method',
  1420. '%+' => '.to method',
  1421. '%^H' => '$?FOO variables',
  1422. ;
  1423. method message() {
  1424. my $name = $!name;
  1425. my $v = $name ~~ m/ <[ $ @ % & ]> [ \^ <[ A..Z ]> | \W ] /;
  1426. my $sugg = %m{~$v};
  1427. if $!identifier-name and $name eq '$#' {
  1428. # Currently only `$#` var has this identifier business handling as
  1429. # there are two versions of it: $# (number formatting) and $#var
  1430. # https://metacpan.org/pod/perlvar#Deprecated-and-removed-variables
  1431. # Should generalize the logic if we get more of stuff like this.
  1432. $name ~= $!identifier-name;
  1433. $sugg = '@' ~ $!identifier-name ~ '.end';
  1434. }
  1435. $v
  1436. ?? $sugg
  1437. ?? "Unsupported use of $name variable; in Perl 6 please use $sugg"
  1438. !! "Unsupported use of $name variable"
  1439. !! 'Weird unrecognized variable name: ' ~ $name;
  1440. }
  1441. }
  1442. my class X::Syntax::Self::WithoutObject does X::Syntax {
  1443. method message() { "'self' used where no object is available" }
  1444. }
  1445. my class X::Syntax::VirtualCall does X::Syntax {
  1446. has $.call;
  1447. method message() { "Virtual method call $.call may not be used on partially constructed object (maybe you mean {$.call.subst('.','!')} for direct attribute access here?)" }
  1448. }
  1449. my class X::Syntax::NoSelf does X::Syntax {
  1450. has $.variable;
  1451. method message() { "Variable $.variable used where no 'self' is available" }
  1452. }
  1453. my class X::Syntax::Number::RadixOutOfRange does X::Syntax {
  1454. has $.radix;
  1455. method message() { "Radix $.radix out of range (allowed: 2..36)" }
  1456. }
  1457. my class X::Syntax::Number::IllegalDecimal does X::Syntax {
  1458. method message() { "Decimal point must be followed by digit" }
  1459. }
  1460. my class X::Syntax::Number::LiteralType does X::Syntax {
  1461. has $.varname;
  1462. has $.vartype;
  1463. has $.value;
  1464. has $.valuetype;
  1465. has $.suggestiontype;
  1466. has $.native;
  1467. method message() {
  1468. my $vartype := $!vartype.WHAT.^name;
  1469. my $conversionmethod := $vartype;
  1470. $vartype := $vartype.lc if $.native;
  1471. my $vt := $!value.^name;
  1472. my $value := $vt eq "IntStr" || $vt eq "NumStr" || $vt eq "RatStr" || $vt eq "ComplexStr"
  1473. ?? $!value.Str
  1474. !! $!value.perl;
  1475. my $val = "Cannot assign a literal of type {$.valuetype} ($value) to { $.native ?? "a native" !! "a" } variable of type $vartype. You can declare the variable to be of type $.suggestiontype, or try to coerce the value with { $value ~ '.' ~ $conversionmethod } or $conversionmethod\($value\)";
  1476. try $val ~= ", or just write the value as " ~ $!value."$vartype"().perl;
  1477. $val;
  1478. }
  1479. }
  1480. my class X::Syntax::NonAssociative does X::Syntax {
  1481. has $.left;
  1482. has $.right;
  1483. method message() {
  1484. "Operators '$.left' and '$.right' are non-associative and require parentheses";
  1485. }
  1486. }
  1487. my class X::Syntax::NonListAssociative is X::Syntax::NonAssociative {
  1488. method message() {
  1489. "Only identical operators may be list associative; since '$.left' and '$.right' differ, they are non-associative and you need to clarify with parentheses";
  1490. }
  1491. }
  1492. my class X::Syntax::CannotMeta does X::Syntax {
  1493. has $.meta;
  1494. has $.operator;
  1495. has $.reason;
  1496. has $.dba;
  1497. method message() {
  1498. "Cannot $.meta $.operator because $.dba operators are $.reason";
  1499. }
  1500. }
  1501. my class X::Syntax::Adverb does X::Syntax {
  1502. has $.what;
  1503. method message() { "You can't adverb " ~ $.what }
  1504. }
  1505. my class X::Syntax::Regex::Adverb does X::Syntax {
  1506. has $.adverb;
  1507. has $.construct;
  1508. method message() { "Adverb $.adverb not allowed on $.construct" }
  1509. }
  1510. my class X::Syntax::Regex::UnrecognizedMetachar does X::Syntax {
  1511. has $.metachar;
  1512. method message() { "Unrecognized regex metacharacter $.metachar (must be quoted to match literally)" }
  1513. }
  1514. my class X::Syntax::Regex::UnrecognizedModifier does X::Syntax {
  1515. has $.modifier;
  1516. method message() { "Unrecognized regex modifier :$.modifier" }
  1517. }
  1518. my class X::Syntax::Regex::NullRegex does X::Syntax {
  1519. method message() { 'Null regex not allowed' }
  1520. }
  1521. my class X::Syntax::Regex::MalformedRange does X::Syntax {
  1522. method message() {
  1523. 'Malformed Range. If attempting to use variables for end points, '
  1524. ~ 'wrap the entire range in curly braces.'
  1525. }
  1526. }
  1527. my class X::Syntax::Regex::Unspace does X::Syntax {
  1528. has $.char;
  1529. method message { "No unspace allowed in regex; if you meant to match the literal character, " ~
  1530. "please enclose in single quotes ('" ~ $.char ~ "') or use a backslashed form like \\x" ~
  1531. sprintf('%02x', $.char.ord)
  1532. }
  1533. }
  1534. my class X::Syntax::Regex::Unterminated does X::Syntax {
  1535. method message { 'Regex not terminated.' }
  1536. }
  1537. my class X::Syntax::Regex::SpacesInBareRange does X::Syntax {
  1538. method message { 'Spaces not allowed in bare range.' }
  1539. }
  1540. my class X::Syntax::Regex::QuantifierValue does X::Syntax {
  1541. has $.inf;
  1542. has $.non-numeric;
  1543. has $.non-numeric-range;
  1544. has $.empty-range;
  1545. method message {
  1546. $!inf
  1547. && 'Minimum quantity to match for quantifier cannot be Inf.'
  1548. ~ ' Did you mean to use + or * quantifiers instead of **?'
  1549. || $!non-numeric-range
  1550. && 'Cannot use Range with non-Numeric or NaN end points as quantifier'
  1551. || $!non-numeric
  1552. && 'Cannot use non-Numeric or NaN value as quantifier'
  1553. || $!empty-range
  1554. && 'Cannot use empty Range as quantifier'
  1555. || 'Invalid quantifier value'
  1556. }
  1557. }
  1558. my class X::Syntax::Regex::SolitaryQuantifier does X::Syntax {
  1559. method message { 'Quantifier quantifies nothing' }
  1560. }
  1561. my class X::Syntax::Regex::NonQuantifiable does X::Syntax {
  1562. method message { 'Can only quantify a construct that produces a match' }
  1563. }
  1564. my class X::Syntax::Regex::SolitaryBacktrackControl does X::Syntax {
  1565. method message { "Backtrack control ':' does not seem to have a preceding atom to control" }
  1566. }
  1567. my class X::Syntax::Regex::Alias::LongName does X::Syntax {
  1568. method message() { "Can only alias to a short name (without '::')"; }
  1569. }
  1570. my class X::Syntax::Term::MissingInitializer does X::Syntax {
  1571. method message { 'Term definition requires an initializer' }
  1572. }
  1573. my class X::Syntax::Variable::MissingInitializer does X::Syntax {
  1574. has $.type;
  1575. has $.implicit;
  1576. method message {
  1577. $.implicit ??
  1578. "Variable definition of type $.type (implicit $.implicit) requires an initializer" !!
  1579. "Variable definition of type $.type requires an initializer"
  1580. }
  1581. }
  1582. my class X::Syntax::AddCategorical::TooFewParts does X::Syntax {
  1583. has $.category;
  1584. has $.needs;
  1585. method message() { "Not enough symbols provided for categorical of type $.category; needs $.needs" }
  1586. }
  1587. my class X::Syntax::AddCategorical::TooManyParts does X::Syntax {
  1588. has $.category;
  1589. has $.needs;
  1590. method message() { "Too many symbols provided for categorical of type $.category; needs only $.needs" }
  1591. }
  1592. my class X::Syntax::Signature::InvocantMarker does X::Syntax {
  1593. method message() {
  1594. "Can only use : as invocant marker in a signature after the first parameter"
  1595. }
  1596. }
  1597. my class X::Syntax::Signature::InvocantNotAllowed does X::Syntax {
  1598. method message() {
  1599. "Can only use the : invocant marker in the signature for a method"
  1600. }
  1601. }
  1602. my class X::Syntax::Extension::Category does X::Syntax {
  1603. has $.category;
  1604. method message() {
  1605. "Cannot add tokens of category '$.category'";
  1606. }
  1607. }
  1608. my class X::Syntax::Extension::Null does X::Syntax {
  1609. method message() {
  1610. "Null operator is not allowed";
  1611. }
  1612. }
  1613. my class X::Syntax::Extension::TooComplex does X::Syntax {
  1614. has $.name;
  1615. method message() {
  1616. "Colon pair value '$.name' too complex to use in name";
  1617. }
  1618. }
  1619. my class X::Syntax::Coercer::TooComplex does X::Syntax {
  1620. method message() {
  1621. 'Coercer is too complex. Only type objects, with optional type'
  1622. ~ " smileys, or empty parentheses, implying 'Any', are supported."
  1623. }
  1624. }
  1625. my class X::Syntax::Extension::SpecialForm does X::Syntax {
  1626. has $.category;
  1627. has $.opname;
  1628. has $.hint;
  1629. method message() {
  1630. "Cannot override $.category operator '$.opname', as it is a special form " ~
  1631. "handled directly by the compiler" ~ ($!hint ?? "\n$!hint" !! "")
  1632. }
  1633. }
  1634. my class X::Syntax::InfixInTermPosition does X::Syntax {
  1635. has $.infix;
  1636. method message() {
  1637. my $infix := $!infix.trim;
  1638. "Preceding context expects a term, but found infix $infix instead."
  1639. ~ (
  1640. $.post && $.post.starts-with('end ')
  1641. ?? "\nDid you forget '=begin $.post.substr(4)' Pod marker?"
  1642. !! "\nDid you make a mistake in Pod syntax?"
  1643. if $infix eq '='
  1644. )
  1645. }
  1646. }
  1647. my class X::Syntax::DuplicatedPrefix does X::Syntax {
  1648. has $.prefixes;
  1649. method message() {
  1650. my $prefix = substr($.prefixes,0,1);
  1651. "Expected a term, but found either infix $.prefixes or redundant prefix $prefix\n"
  1652. ~ " (to suppress this message, please use a space like $prefix $prefix)";
  1653. }
  1654. }
  1655. my class X::Attribute::Package does X::Comp {
  1656. has $.package-kind;
  1657. has $.name;
  1658. method message() { "A $.package-kind cannot have attributes, but you tried to declare '$.name'" }
  1659. }
  1660. my class X::Attribute::NoPackage does X::Comp {
  1661. has $.name;
  1662. method message() { "You cannot declare attribute '$.name' here; maybe you'd like a class or a role?" }
  1663. }
  1664. my class X::Attribute::Required does X::MOP {
  1665. has $.name;
  1666. has $.why;
  1667. method message() {
  1668. $.why && nqp::istype($.why,Str)
  1669. ?? "The attribute '$.name' is required because $.why,\nbut you did not provide a value for it."
  1670. !! "The attribute '$.name' is required, but you did not provide a value for it."
  1671. }
  1672. }
  1673. my class X::Attribute::Scope::Package does X::Comp {
  1674. has $.scope;
  1675. has $.allowed;
  1676. has $.disallowed;
  1677. method message() { "Cannot use {$.scope}-scoped attribute in $.disallowed"
  1678. ~ ($.allowed ?? ", only $.allowed." !! ".") }
  1679. }
  1680. my class X::Declaration::Scope does X::Comp {
  1681. has $.scope;
  1682. has $.declaration;
  1683. method message() { "Cannot use '$.scope' with $.declaration declaration" }
  1684. }
  1685. my class X::Declaration::Scope::Multi is X::Declaration::Scope {
  1686. method message() {
  1687. "Cannot use '$.scope' with individual multi candidates. Please declare an {$.scope}-scoped proto instead";
  1688. }
  1689. }
  1690. my class X::Declaration::OurScopeInRole does X::Comp {
  1691. has $.declaration;
  1692. method message() {
  1693. "Cannot declare our-scoped $.declaration inside of a role\n" ~
  1694. "(the scope inside of a role is generic, so there is no unambiguous\n" ~
  1695. "package to install the symbol in)"
  1696. }
  1697. }
  1698. my class X::Anon::Multi does X::Comp {
  1699. has $.multiness;
  1700. has $.routine-type = 'routine';
  1701. method message() { "An anonymous $.routine-type may not take a $.multiness declarator" }
  1702. }
  1703. my class X::Anon::Augment does X::Comp {
  1704. has $.package-kind;
  1705. method message() { "Cannot augment anonymous $.package-kind" }
  1706. }
  1707. my class X::Augment::NoSuchType does X::Comp {
  1708. has $.package-kind;
  1709. has $.package;
  1710. method message() { "You tried to augment $.package-kind $.package, but it does not exist" }
  1711. }
  1712. my class X::Routine::Unwrap is Exception {
  1713. method message() { "Cannot unwrap routine: invalid wrap handle" }
  1714. }
  1715. my class X::Constructor::Positional is Exception {
  1716. has $.type;
  1717. method message() { "Default constructor for '" ~ $.type.^name ~ "' only takes named arguments" }
  1718. }
  1719. my class X::Hash::Store::OddNumber is Exception {
  1720. has $.found;
  1721. has $.last;
  1722. method message() {
  1723. my $msg =
  1724. "Odd number of elements found where hash initializer expected";
  1725. if $.found == 1 {
  1726. $msg ~= $.last
  1727. ?? ":\nOnly saw: $.last.perl()"
  1728. !! ":\nOnly saw 1 element"
  1729. }
  1730. else {
  1731. $msg ~= ":\nFound $.found (implicit) elements";
  1732. $msg ~= ":\nLast element seen: $.last.perl()" if $.last;
  1733. }
  1734. }
  1735. }
  1736. my class X::Pairup::OddNumber is Exception {
  1737. method message() { "Odd number of elements found for .pairup()" }
  1738. }
  1739. my class X::Match::Bool is Exception {
  1740. has $.type;
  1741. method message() { "Cannot use Bool as Matcher with '" ~ $.type ~ "'. Did you mean to use \$_ inside a block?" }
  1742. }
  1743. my class X::LibEmpty does X::Comp {
  1744. method message { q/Repository specification can not be an empty string. Did you mean 'use lib "."' ?/ }
  1745. }
  1746. my class X::LibNone does X::Comp {
  1747. method message { q/Must specify at least one repository. Did you mean 'use lib "lib"' ?/ }
  1748. }
  1749. my class X::Package::UseLib does X::Comp {
  1750. has $.what;
  1751. method message { "Cannot 'use lib' inside a $.what" }
  1752. }
  1753. my class X::Package::Stubbed does X::Comp {
  1754. has @.packages;
  1755. method message() {
  1756. "The following packages were stubbed but not defined:\n "
  1757. ~ @.packages.join("\n ");
  1758. }
  1759. # The unnamed named param is here so this candidate, rather than
  1760. # the one from X::Comp is used. (is it a bug that this is needed?
  1761. # No idea: https://irclog.perlgeek.de/perl6-dev/2017-09-14#i_15164569 )
  1762. multi method gist(::?CLASS:D: :$) {
  1763. $.message;
  1764. }
  1765. }
  1766. my class X::Phaser::PrePost is Exception {
  1767. has $.phaser = 'PRE';
  1768. has $.condition;
  1769. method message {
  1770. my $what = $.phaser eq 'PRE' ?? 'Precondition' !! 'Postcondition';
  1771. $.condition.defined
  1772. ?? "$what '$.condition.trim()' failed"
  1773. !! "$what failed";
  1774. }
  1775. }
  1776. my class X::Str::InvalidCharName is Exception {
  1777. has $.name;
  1778. method message() {
  1779. $!name.chars ?? "Unrecognized character name [{$!name}]"
  1780. !! "Cannot use empty name as character name"
  1781. }
  1782. }
  1783. my class X::Str::Numeric is Exception {
  1784. has $.source;
  1785. has $.pos;
  1786. has $.reason;
  1787. method source-indicator {
  1788. my ($red,$clear,$green,$,$eject) = Rakudo::Internals.error-rcgye;
  1789. my sub escape($str) { $str.perl.substr(1).chop }
  1790. join '', "in '",
  1791. $green,
  1792. escape(substr($.source,0, $.pos)),
  1793. $eject,
  1794. $red,
  1795. escape(substr($.source,$.pos)),
  1796. $clear,
  1797. "' (indicated by ",
  1798. $eject,
  1799. $clear,
  1800. ")",
  1801. ;
  1802. }
  1803. method message() {
  1804. "Cannot convert string to number: $.reason $.source-indicator";
  1805. }
  1806. }
  1807. my class X::Str::Match::x is Exception {
  1808. has $.got is default(Nil);
  1809. method message() {
  1810. "in Str.match, got invalid value of type {$.got.^name} for :x, must be Int or Range"
  1811. }
  1812. }
  1813. my class X::Str::Subst::Adverb is Exception {
  1814. has $.name;
  1815. has $.got;
  1816. method message() {
  1817. "Cannot use :$.name adverb in Str.subst, got $.got"
  1818. }
  1819. }
  1820. my class X::Str::Trans::IllegalKey is Exception {
  1821. has $.key;
  1822. method message {
  1823. "in Str.trans, got illegal substitution key of type {$.key.^name} (should be a Regex or Str)"
  1824. }
  1825. }
  1826. my class X::Str::Trans::InvalidArg is Exception {
  1827. has $.got is default(Nil);
  1828. method message() {
  1829. "Only Pair objects are allowed as arguments to Str.trans, got {$.got.^name}";
  1830. }
  1831. }
  1832. my class X::Str::Sprintf::Directives::Count is Exception {
  1833. has int $.args-used;
  1834. has num $.args-have;
  1835. method message() {
  1836. "Your printf-style directives specify "
  1837. ~ ($.args-used == 1 ?? "1 argument, but "
  1838. !! "$.args-used arguments, but ")
  1839. ~ ($.args-have < 1 ?? "no argument was"
  1840. !! $.args-have == 1 ?? "1 argument was"
  1841. !! "$.args-have arguments were")
  1842. ~ " supplied";
  1843. }
  1844. }
  1845. my class X::Str::Sprintf::Directives::Unsupported is Exception {
  1846. has str $.directive;
  1847. has str $.sequence;
  1848. method message() {
  1849. "Directive $.directive is not valid in sprintf format sequence $.sequence"
  1850. }
  1851. }
  1852. my class X::Str::Sprintf::Directives::BadType is Exception {
  1853. has str $.type;
  1854. has str $.directive;
  1855. method message() {
  1856. "Directive $.directive not applicable for type $.type"
  1857. }
  1858. }
  1859. my role X::Encoding is Exception { }
  1860. my class X::Encoding::Unknown does X::Encoding {
  1861. has $.name;
  1862. method message() {
  1863. "Unknown string encoding '$.name'"
  1864. }
  1865. }
  1866. my class X::Encoding::AlreadyRegistered does X::Encoding {
  1867. has $.name;
  1868. method message() {
  1869. "An encoding with name '$.name' has already been registered"
  1870. }
  1871. }
  1872. my class X::Range::InvalidArg is Exception {
  1873. has $.got is default(Nil);
  1874. method message() {
  1875. "{$.got.^name} objects are not valid endpoints for Ranges";
  1876. }
  1877. }
  1878. my class X::Sequence::Deduction is Exception {
  1879. has $.from;
  1880. method message() {
  1881. $!from ?? "Unable to deduce arithmetic or geometric sequence from $!from (or did you really mean '..'?)"
  1882. !! 'Unable to deduce sequence for some unfathomable reason'
  1883. }
  1884. }
  1885. my class X::Cannot::Lazy is Exception {
  1886. has $.action;
  1887. has $.what;
  1888. method message() {
  1889. $.what
  1890. ?? "Cannot $.action a lazy list onto a $.what"
  1891. !! "Cannot $.action a lazy list";
  1892. }
  1893. }
  1894. my class X::Cannot::Empty is Exception {
  1895. has $.action;
  1896. has $.what;
  1897. method message() {
  1898. "Cannot $.action from an empty $.what";
  1899. }
  1900. }
  1901. my class X::Cannot::New is Exception {
  1902. has $.class;
  1903. method message() {
  1904. "Cannot make a {$.class.^name} object using .new";
  1905. }
  1906. }
  1907. my class X::Cannot::Capture is Exception {
  1908. has $.what;
  1909. method message() {
  1910. "Cannot unpack or Capture `$!what.gist()`.\n"
  1911. ~ "To create a Capture, add parentheses: \\(...)\n"
  1912. ~ 'If unpacking in a signature, perhaps you needlessly used'
  1913. ~ ' parentheses? -> ($x) {} vs. -> $x {}'
  1914. ~ "\nor missed `:` in signature unpacking? -> \&c:(Int) \{}";
  1915. }
  1916. }
  1917. my class X::Backslash::UnrecognizedSequence does X::Syntax {
  1918. has $.sequence;
  1919. method message() { "Unrecognized backslash sequence: '\\$.sequence'" }
  1920. }
  1921. my class X::Backslash::NonVariableDollar does X::Syntax {
  1922. method message() { "Non-variable \$ must be backslashed" }
  1923. }
  1924. my class X::ControlFlow is Exception {
  1925. has $.illegal; # something like 'next'
  1926. has $.enclosing; # .... outside a loop
  1927. has $.backtrace; # where the bogus control flow op was
  1928. method backtrace() {
  1929. $!backtrace || nextsame();
  1930. }
  1931. method message() { "$.illegal without $.enclosing" }
  1932. }
  1933. my class X::ControlFlow::Return is X::ControlFlow {
  1934. has Bool $.out-of-dynamic-scope;
  1935. submethod BUILD(Bool() :$!out-of-dynamic-scope) {}
  1936. method illegal() { 'return' }
  1937. method enclosing() { 'Routine' }
  1938. method message() {
  1939. 'Attempt to return outside of ' ~ (
  1940. $!out-of-dynamic-scope
  1941. ?? 'immediatelly-enclosing Routine (i.e. `return` execution is'
  1942. ~ ' outside the dynamic scope of the Routine where `return` was used)'
  1943. !! 'any Routine'
  1944. )
  1945. }
  1946. }
  1947. my class X::Composition::NotComposable does X::Comp {
  1948. has $.target-name;
  1949. has $.composer;
  1950. method message() {
  1951. $!composer.^name ~ " is not composable, so $!target-name cannot compose it";
  1952. }
  1953. }
  1954. my class X::TypeCheck is Exception {
  1955. has $.operation;
  1956. has $.got is default(Nil);
  1957. has $.expected is default(Nil);
  1958. method gotn() {
  1959. my $perl = (try $!got.perl) // "?";
  1960. $perl = "$perl.substr(0,21)..." if $perl.chars > 24;
  1961. (try $!got.^name eq $!expected.^name
  1962. ?? $perl
  1963. !! "$!got.^name() ($perl)"
  1964. ) // "?"
  1965. }
  1966. method expectedn() {
  1967. (try $!got.^name eq $!expected.^name
  1968. ?? $!expected.perl
  1969. !! $!expected.^name
  1970. ) // "?"
  1971. }
  1972. method priors() {
  1973. (try nqp::isconcrete($!got) && $!got ~~ Failure)
  1974. ?? "Earlier failure:\n " ~ $!got.mess ~ "\nFinal error:\n "
  1975. !! ''
  1976. }
  1977. method message() {
  1978. self.priors() ~
  1979. "Type check failed in $.operation; expected $.expectedn but got $.gotn";
  1980. }
  1981. }
  1982. my class X::TypeCheck::Binding is X::TypeCheck {
  1983. has $.symbol;
  1984. method operation { 'binding' }
  1985. method message() {
  1986. my $to = $.symbol.defined && $.symbol ne '$'
  1987. ?? " to '$.symbol'"
  1988. !! "";
  1989. my $expected = (try nqp::eqaddr($.expected,$.got))
  1990. ?? "expected type $.expectedn cannot be itself"
  1991. !! "expected $.expectedn but got $.gotn";
  1992. self.priors() ~ "Type check failed in $.operation$to; $expected";
  1993. }
  1994. }
  1995. my class X::TypeCheck::Binding::Parameter is X::TypeCheck::Binding {
  1996. has Parameter $.parameter;
  1997. has Bool $.constraint;
  1998. method expectedn() {
  1999. $.constraint && $.expected ~~ Code
  2000. ?? 'anonymous constraint to be met'
  2001. !! callsame()
  2002. }
  2003. method message() {
  2004. my $to = $.symbol.defined && $.symbol ne '$'
  2005. ?? " to parameter '$.symbol'"
  2006. !! " to anonymous parameter";
  2007. my $expected = (try nqp::eqaddr($.expected,$.got))
  2008. ?? "expected type $.expectedn cannot be itself"
  2009. !! "expected $.expectedn but got $.gotn";
  2010. my $what-check = $.constraint ?? 'Constraint type' !! 'Type';
  2011. self.priors() ~ "$what-check check failed in $.operation$to; $expected";
  2012. }
  2013. }
  2014. my class X::TypeCheck::Return is X::TypeCheck {
  2015. method operation { 'returning' }
  2016. method message() {
  2017. my $expected = $.expected =:= $.got
  2018. ?? "expected return type $.expectedn cannot be itself " ~
  2019. "(perhaps $.operation a :D type object?)"
  2020. !! "expected $.expectedn but got $.gotn";
  2021. self.priors() ~
  2022. "Type check failed for return value; $expected";
  2023. }
  2024. }
  2025. my class X::TypeCheck::Assignment is X::TypeCheck {
  2026. has $.symbol;
  2027. method operation { 'assignment' }
  2028. method message {
  2029. my $to = $.symbol.defined && $.symbol ne '$'
  2030. ?? " to $.symbol" !! "";
  2031. my $expected = $.expected =:= $.got
  2032. ?? "expected type $.expectedn cannot be itself " ~
  2033. "(perhaps Nil was assigned to a :D which had no default?)"
  2034. !! "expected $.expectedn but got $.gotn";
  2035. self.priors() ~ "Type check failed in assignment$to; $expected";
  2036. }
  2037. }
  2038. my class X::TypeCheck::Argument is X::TypeCheck {
  2039. has $.protoguilt;
  2040. has @.arguments;
  2041. has $.objname;
  2042. has $.signature;
  2043. method message {
  2044. my $multi = $!signature ~~ /\n/ // '';
  2045. "Calling {$!objname}({ join(', ', @!arguments) }) will never work with " ~ (
  2046. $!protoguilt ?? 'proto signature ' !!
  2047. $multi ?? 'any of these multi signatures:' !!
  2048. 'declared signature '
  2049. ) ~ $!signature;
  2050. }
  2051. }
  2052. my class X::TypeCheck::Splice is X::TypeCheck does X::Comp {
  2053. has $.action;
  2054. method message {
  2055. self.priors() ~
  2056. "Type check failed in {$.action}; expected $.expectedn but got $.gotn";
  2057. }
  2058. }
  2059. my class X::Assignment::RO is Exception {
  2060. has $.value = "value";
  2061. method message {
  2062. "Cannot modify an immutable {$!value.^name} ({
  2063. Rakudo::Internals.SHORT-GIST: $!value
  2064. })"
  2065. }
  2066. method typename { $.value.^name }
  2067. }
  2068. my class X::Assignment::RO::Comp does X::Comp {
  2069. has $.variable;
  2070. method message {
  2071. "Cannot assign to readonly variable {$.variable}"
  2072. }
  2073. }
  2074. my class X::Immutable is Exception {
  2075. has $.typename;
  2076. has $.method;
  2077. method message {
  2078. "Cannot call '$.method' on an immutable '$.typename'";
  2079. }
  2080. }
  2081. my class X::NoDispatcher is Exception {
  2082. has $.redispatcher;
  2083. method message() {
  2084. "$.redispatcher is not in the dynamic scope of a dispatcher";
  2085. }
  2086. }
  2087. my class X::Localizer::NoContainer is Exception {
  2088. has $.localizer;
  2089. method message() {
  2090. "Can only use '$.localizer' on a container";
  2091. }
  2092. }
  2093. my class X::Mixin::NotComposable is Exception {
  2094. has $.target;
  2095. has $.rolish;
  2096. method message() {
  2097. "Cannot mix in non-composable type {$.rolish.^name} into object of type {$.target.^name}";
  2098. }
  2099. }
  2100. my class X::Inheritance::Unsupported does X::Comp {
  2101. # note that this exception is thrown before the child type object
  2102. # has been composed, so it's useless to carry it around. Use the
  2103. # name instead.
  2104. has $.child-typename;
  2105. has $.parent;
  2106. method message {
  2107. $!parent.^name ~ ' does not support inheritance, so '
  2108. ~ $!child-typename ~ ' cannot inherit from it';
  2109. }
  2110. }
  2111. my class X::Inheritance::UnknownParent is Exception {
  2112. has $.child;
  2113. has $.parent;
  2114. has @.suggestions is rw;
  2115. method message {
  2116. my $message := "'" ~ $.child ~ "' cannot inherit from '" ~ $.parent ~ "' because it is unknown.";
  2117. if +@.suggestions > 1 {
  2118. $message := $message ~ "\nDid you mean one of these?\n '" ~ @.suggestions.join("'\n '") ~ "'\n";
  2119. } elsif +@.suggestions == 1 {
  2120. $message := $message ~ "\nDid you mean '" ~ @.suggestions[0] ~ "'?\n";
  2121. }
  2122. $message;
  2123. }
  2124. }
  2125. my class X::Inheritance::SelfInherit is Exception {
  2126. has $.name;
  2127. method message {
  2128. "'$.name' cannot inherit from itself."
  2129. }
  2130. }
  2131. my class X::Export::NameClash does X::Comp {
  2132. has $.symbol;
  2133. method message() {
  2134. "A symbol '$.symbol' has already been exported";
  2135. }
  2136. }
  2137. my class X::HyperOp::NonDWIM is Exception {
  2138. has &.operator;
  2139. has $.left-elems;
  2140. has $.right-elems;
  2141. has $.recursing;
  2142. method message() {
  2143. "Lists on either side of non-dwimmy hyperop of &.operator.name() are not of the same length"
  2144. ~ " while recursing" x +$.recursing
  2145. ~ "\nleft: $.left-elems elements, right: $.right-elems elements";
  2146. }
  2147. }
  2148. my class X::HyperOp::Infinite is Exception {
  2149. has &.operator;
  2150. has $.side;
  2151. method message() {
  2152. $.side eq "both"
  2153. ?? "Lists on both sides of hyperop of &.operator.name() are known to be infinite"
  2154. !! "List on $.side side of hyperop of &.operator.name() is known to be infinite"
  2155. }
  2156. }
  2157. my class X::Set::Coerce is Exception {
  2158. has $.thing;
  2159. method message {
  2160. "Cannot coerce object of type {$.thing.^name} to Set. To create a one-element set, pass it to the 'set' function";
  2161. }
  2162. }
  2163. my role X::Temporal is Exception { }
  2164. my class X::Temporal::InvalidFormat does X::Temporal {
  2165. has $.invalid-str;
  2166. has $.target = 'Date';
  2167. has $.format;
  2168. method message() {
  2169. "Invalid $.target string '$.invalid-str'; use $.format instead";
  2170. }
  2171. }
  2172. my class X::DateTime::TimezoneClash does X::Temporal {
  2173. method message() {
  2174. 'DateTime.new(Str): :timezone argument not allowed with a timestamp offset';
  2175. }
  2176. }
  2177. my class X::DateTime::InvalidDeltaUnit does X::Temporal {
  2178. has $.unit;
  2179. method message() {
  2180. "Cannot use unit $.unit with Date.delta";
  2181. }
  2182. }
  2183. my class X::Eval::NoSuchLang is Exception {
  2184. has $.lang;
  2185. method message() {
  2186. "No compiler available for language '$.lang'";
  2187. }
  2188. }
  2189. my class X::Import::MissingSymbols is Exception {
  2190. has $.from;
  2191. has @.missing;
  2192. method message() {
  2193. "Trying to import from '$.from', but the following symbols are missing: "
  2194. ~ @.missing.join(', ');
  2195. }
  2196. }
  2197. my class X::Import::NoSuchTag is Exception {
  2198. has $.source-package;
  2199. has $.tag;
  2200. method message() {
  2201. "Error while importing from '$.source-package': no such tag '$.tag'"
  2202. }
  2203. }
  2204. my class X::Import::Positional is Exception {
  2205. has $.source-package;
  2206. method message() {
  2207. "Error while importing from '$.source-package':\n"
  2208. ~ "no EXPORT sub, but you provided positional argument in the 'use' statement"
  2209. }
  2210. }
  2211. my class X::Numeric::CannotConvert is Exception {
  2212. has $.target;
  2213. has $.reason;
  2214. has $.source;
  2215. method message() {
  2216. "Cannot convert $!source to {$!target // $!target.perl}: $!reason";
  2217. }
  2218. }
  2219. my class X::Numeric::Real is X::Numeric::CannotConvert {}
  2220. my class X::Numeric::DivideByZero is Exception {
  2221. has $.using;
  2222. has $.details;
  2223. has $.numerator;
  2224. method message() {
  2225. "Attempt to divide{$.numerator ?? " $.numerator" !! ''} by zero"
  2226. ~ ( $.using ?? " using $.using" !! '' )
  2227. ~ ( " $_" with $.details );
  2228. }
  2229. }
  2230. my class X::Numeric::Overflow is Exception {
  2231. method message() { "Numeric overflow" }
  2232. }
  2233. my class X::Numeric::Underflow is Exception {
  2234. method message() { "Numeric underflow" }
  2235. }
  2236. my class X::Numeric::Confused is Exception {
  2237. has $.num;
  2238. has $.base;
  2239. method message() {
  2240. "This call only converts base-$.base strings to numbers; value "
  2241. ~ "{$.num.perl} is of type {$.num.WHAT.^name}, so cannot be converted!"
  2242. ~ (
  2243. "\n(If you really wanted to convert {$.num.perl} to a base-$.base"
  2244. ~ " string, use {$.num.perl}.base($.base) instead.)"
  2245. if $.num.^can('base')
  2246. );
  2247. }
  2248. }
  2249. my class X::PseudoPackage::InDeclaration does X::Comp {
  2250. has $.pseudo-package;
  2251. has $.action;
  2252. method message() {
  2253. "Cannot use pseudo package $.pseudo-package in $.action";
  2254. }
  2255. }
  2256. my class X::NoSuchSymbol is Exception {
  2257. has $.symbol;
  2258. method message { "No such symbol '$.symbol'" }
  2259. }
  2260. my class X::Item is Exception {
  2261. has $.aggregate;
  2262. has $.index;
  2263. method message { "Cannot index {$.aggregate.^name} with $.index" }
  2264. }
  2265. my class X::Multi::Ambiguous is Exception {
  2266. has $.dispatcher;
  2267. has @.ambiguous;
  2268. has $.capture;
  2269. method message {
  2270. my @bits;
  2271. my @priors;
  2272. if $.capture {
  2273. for $.capture.list {
  2274. try @bits.push(.WHAT.perl);
  2275. @bits.push($_.^name) if $!;
  2276. when Failure {
  2277. @priors.push(" " ~ .mess);
  2278. }
  2279. }
  2280. for $.capture.hash {
  2281. if .value ~~ Failure {
  2282. @priors.push(" " ~ .value.mess);
  2283. }
  2284. if .value ~~ Bool {
  2285. @bits.push(':' ~ ('!' x !.value) ~ .key);
  2286. }
  2287. else {
  2288. try @bits.push(":$(.key)\($(.value.WHAT.?perl))");
  2289. @bits.push(':' ~ .value.^name) if $!;
  2290. }
  2291. }
  2292. }
  2293. else {
  2294. @bits.push('...');
  2295. }
  2296. if @.ambiguous[0].signature.gist.contains: ': ' {
  2297. my $invocant = @bits.shift;
  2298. my $first = @bits ?? @bits.shift !! '';
  2299. @bits.unshift($invocant ~ ': ' ~ $first);
  2300. }
  2301. my $cap = '(' ~ @bits.join(", ") ~ ')';
  2302. @priors = flat "Earlier failures:\n", @priors, "\nFinal error:\n " if @priors;
  2303. @priors.join ~ join "\n",
  2304. "Ambiguous call to '$.dispatcher.name()$cap'; these signatures all match:",
  2305. @.ambiguous.map(*.signature.perl)
  2306. }
  2307. }
  2308. my class X::Multi::NoMatch is Exception {
  2309. has $.dispatcher;
  2310. has $.capture;
  2311. method message {
  2312. my @cand = $.dispatcher.dispatchees.map(*.signature.gist);
  2313. my @un-rw-cand;
  2314. if first / 'is rw' /, @cand {
  2315. my $rw-capture = Capture.new(
  2316. :list( $!capture.list.map({ my $ = $_ }) ),
  2317. :hash( $!capture.hash.map({ .key => my $ = .value }).hash ),
  2318. );
  2319. @un-rw-cand = $.dispatcher.dispatchees».signature.grep({
  2320. $rw-capture ~~ $^cand
  2321. })».gist;
  2322. }
  2323. my $where = so first / where /, @cand;
  2324. my @bits;
  2325. my @priors;
  2326. if $.capture {
  2327. for $.capture.list {
  2328. try @bits.push(
  2329. $where ?? Rakudo::Internals.SHORT-GIST($_) !! .WHAT.perl
  2330. );
  2331. @bits.push($_.^name) if $!;
  2332. when Failure {
  2333. @priors.push(" " ~ .mess);
  2334. }
  2335. }
  2336. for $.capture.hash {
  2337. if .value ~~ Failure {
  2338. @priors.push(" " ~ .value.mess);
  2339. }
  2340. if .value ~~ Bool {
  2341. @bits.push(':' ~ ('!' x !.value) ~ .key);
  2342. }
  2343. else {
  2344. try @bits.push(":$(.key)\($($where
  2345. ?? Rakudo::Internals.SHORT-GIST: .value
  2346. !! .value.WHAT.?perl
  2347. ))");
  2348. @bits.push(':' ~ .value.^name) if $!;
  2349. }
  2350. }
  2351. }
  2352. else {
  2353. @bits.push('...');
  2354. }
  2355. if @cand && @cand[0] ~~ /': '/ {
  2356. my $invocant = @bits.shift;
  2357. my $first = @bits ?? @bits.shift !! '';
  2358. @bits.unshift($invocant ~ ': ' ~ $first);
  2359. }
  2360. my $cap = '(' ~ @bits.join(", ") ~ ')';
  2361. @priors = flat "Earlier failures:\n", @priors, "\nFinal error:\n " if @priors;
  2362. @priors.join ~ "Cannot resolve caller $.dispatcher.name()$cap; " ~ (
  2363. @un-rw-cand
  2364. ?? "the following candidates\nmatch the type but require "
  2365. ~ 'mutable arguments:' ~ join("\n ", '', @un-rw-cand) ~ (
  2366. "\n\nThe following do not match for other reasons:"
  2367. ~ join("\n ", '', sort keys @cand ∖ @un-rw-cand)
  2368. unless @cand == @un-rw-cand
  2369. )
  2370. !! ( @cand
  2371. ?? join "\n ", 'none of these signatures match:', @cand
  2372. !! "Routine does not have any candidates. Is only the proto defined?"
  2373. )
  2374. );
  2375. }
  2376. }
  2377. my class X::Caller::NotDynamic is Exception {
  2378. has $.symbol;
  2379. method message() {
  2380. "Cannot access '$.symbol' through CALLER, because it is not declared as dynamic";
  2381. }
  2382. }
  2383. my class X::Inheritance::NotComposed does X::MOP {
  2384. # normally, we try very hard to capture the types
  2385. # and not just their names. But in this case, both types
  2386. # involved aren't composed yet, so they basically aren't
  2387. # usable at all.
  2388. has $.child-name;
  2389. has $.parent-name;
  2390. method message() {
  2391. "'$.child-name' cannot inherit from '$.parent-name' because '$.parent-name' isn't composed yet"
  2392. ~ ' (maybe it is stubbed)';
  2393. }
  2394. }
  2395. my class X::PhaserExceptions is Exception {
  2396. has @.exceptions;
  2397. method message() {
  2398. "Multiple exceptions were thrown by LEAVE/POST phasers"
  2399. }
  2400. multi method gist(X::PhaserExceptions:D:) {
  2401. join "\n", flat
  2402. "Multiple exceptions were thrown by LEAVE/POST phasers\n",
  2403. @!exceptions>>.gist>>.indent(4)
  2404. }
  2405. }
  2406. nqp::bindcurhllsym('P6EX', BEGIN nqp::hash(
  2407. 'X::TypeCheck::Binding',
  2408. -> Mu $got, Mu $expected, $symbol? {
  2409. X::TypeCheck::Binding.new(:$got, :$expected, :$symbol).throw;
  2410. },
  2411. 'X::TypeCheck::Binding::Parameter',
  2412. -> Mu $got, Mu $expected, $symbol, $parameter, $is-constraint? {
  2413. my $constraint = $is-constraint ?? True !! False;
  2414. X::TypeCheck::Binding::Parameter.new(:$got, :$expected, :$symbol, :$parameter, :$constraint).throw;
  2415. },
  2416. 'X::TypeCheck::Assignment',
  2417. -> Mu $symbol, Mu $got, Mu $expected {
  2418. X::TypeCheck::Assignment.new(:$symbol, :$got, :$expected).throw;
  2419. },
  2420. 'X::TypeCheck::Return',
  2421. -> Mu $got, Mu $expected {
  2422. X::TypeCheck::Return.new(:$got, :$expected).throw;
  2423. },
  2424. 'X::Assignment::RO',
  2425. -> $value = "value" {
  2426. X::Assignment::RO.new(:$value).throw;
  2427. },
  2428. 'X::ControlFlow::Return',
  2429. -> $out-of-dynamic-scope = False {
  2430. X::ControlFlow::Return.new(:$out-of-dynamic-scope).throw;
  2431. },
  2432. 'X::NoDispatcher',
  2433. -> $redispatcher {
  2434. X::NoDispatcher.new(:$redispatcher).throw;
  2435. },
  2436. 'X::Method::NotFound',
  2437. -> Mu $invocant, $method, $typename, $private = False {
  2438. X::Method::NotFound.new(:$invocant, :$method, :$typename, :$private).throw
  2439. },
  2440. 'X::Multi::Ambiguous',
  2441. -> $dispatcher, @ambiguous, $capture {
  2442. X::Multi::Ambiguous.new(:$dispatcher, :@ambiguous, :$capture).throw
  2443. },
  2444. 'X::Multi::NoMatch',
  2445. -> $dispatcher, $capture {
  2446. X::Multi::NoMatch.new(:$dispatcher, :$capture).throw
  2447. },
  2448. 'X::Role::Initialization',
  2449. -> $role {
  2450. X::Role::Initialization.new(:$role).throw
  2451. },
  2452. 'X::Role::Parametric::NoSuchCandidate',
  2453. -> Mu $role {
  2454. X::Role::Parametric::NoSuchCandidate.new(:$role).throw;
  2455. },
  2456. 'X::Inheritance::NotComposed',
  2457. -> $child-name, $parent-name {
  2458. X::Inheritance::NotComposed.new(:$child-name, :$parent-name).throw;
  2459. },
  2460. 'X::Parameter::RW',
  2461. -> Mu $got, $symbol {
  2462. X::Parameter::RW.new(:$got, :$symbol).throw;
  2463. },
  2464. 'X::PhaserExceptions',
  2465. -> @exceptions {
  2466. X::PhaserExceptions.new(exceptions =>
  2467. @exceptions.map(-> Mu \e { EXCEPTION(e) })).throw;
  2468. },
  2469. 'X::Trait::Invalid',
  2470. -> $type, $subtype, $declaring, $name {
  2471. X::Trait::Invalid.new(:$type, :$subtype, :$declaring, :$name).throw;
  2472. },
  2473. 'X::Parameter::InvalidConcreteness',
  2474. -> $expected, $got, $routine, $param, Bool() $should-be-concrete, Bool() $param-is-invocant {
  2475. X::Parameter::InvalidConcreteness.new(:$expected, :$got, :$routine, :$param, :$should-be-concrete, :$param-is-invocant).throw;
  2476. },
  2477. 'X::NYI',
  2478. -> $feature {
  2479. X::NYI.new(:$feature).throw;
  2480. },
  2481. ));
  2482. my class X::HyperWhatever::Multiple is Exception {
  2483. method message() {
  2484. "Multiple HyperWhatevers and Whatevers may not be used together"
  2485. }
  2486. }
  2487. my class X::EXPORTHOW::InvalidDirective does X::Comp {
  2488. has $.directive;
  2489. method message() {
  2490. "Unknown EXPORTHOW directive '$.directive' encountered during import"
  2491. }
  2492. }
  2493. my class X::EXPORTHOW::NothingToSupersede does X::Comp {
  2494. has $.declarator;
  2495. method message() {
  2496. "There is no package declarator '$.declarator' to supersede"
  2497. }
  2498. }
  2499. my class X::EXPORTHOW::Conflict does X::Comp {
  2500. has $.declarator;
  2501. has $.directive;
  2502. method message() {
  2503. "'EXPORTHOW::{$.directive}::{$.declarator}' conflicts with an existing meta-object imported into this lexical scope"
  2504. }
  2505. }
  2506. my class X::UnitScope::Invalid does X::Syntax {
  2507. has $.what;
  2508. has $.where;
  2509. has Str:D $.suggestion = 'Please use the block form.';
  2510. method message() {
  2511. "A unit-scoped $.what definition is not allowed $.where;\n$!suggestion"
  2512. }
  2513. }
  2514. my class X::UnitScope::TooLate does X::Syntax {
  2515. has $.what;
  2516. method message() {
  2517. "Too late for unit-scoped $.what definition;\n"
  2518. ~ "Please use the block form."
  2519. }
  2520. }
  2521. my class X::StubCode is Exception {
  2522. has $.message = 'Stub code executed';
  2523. }
  2524. my class X::TooLateForREPR is X::Comp {
  2525. has $.type;
  2526. method message() {
  2527. "Cannot change REPR of $!type.^name() now (must be set at initial declaration)";
  2528. }
  2529. }
  2530. my class X::MustBeParametric is Exception {
  2531. has $.type;
  2532. method message() {
  2533. "$!type.^name() *must* be parameterized";
  2534. }
  2535. }
  2536. my class X::NotParametric is Exception {
  2537. has $.type;
  2538. method message() {
  2539. "$!type.^name() cannot be parameterized";
  2540. }
  2541. }
  2542. my class X::InvalidType does X::Comp {
  2543. has $.typename;
  2544. has @.suggestions;
  2545. method message() {
  2546. my $msg := "Invalid typename '$.typename'";
  2547. if +@.suggestions > 0 {
  2548. $msg := $msg ~ ". Did you mean '" ~ @.suggestions.join("', '") ~ "'?";
  2549. }
  2550. $msg;
  2551. }
  2552. }
  2553. my class X::InvalidTypeSmiley does X::Comp {
  2554. has $.name;
  2555. method message() {
  2556. "Invalid type smiley '$.name' used in type name";
  2557. }
  2558. }
  2559. my class X::MultipleTypeSmiley does X::Comp {
  2560. method message() {
  2561. "Multiple type smileys cannot be used";
  2562. }
  2563. }
  2564. my class X::Seq::Consumed is Exception {
  2565. method message() {
  2566. "The iterator of this Seq is already in use/consumed by another Seq\n" ~
  2567. "(you might solve this by adding .cache on usages of the Seq, or\n" ~
  2568. "by assigning the Seq into an array)"
  2569. }
  2570. }
  2571. my class X::Seq::NotIndexable is Exception {
  2572. method message() {
  2573. "Cannot index a Seq; coerce it to a list or assign it to an array first"
  2574. }
  2575. }
  2576. my class X::WheneverOutOfScope is Exception {
  2577. method message() {
  2578. "Cannot have a 'whenever' block outside the scope of a 'supply' or 'react' block"
  2579. }
  2580. }
  2581. my class X::Comp::WheneverOutOfScope does X::Comp {
  2582. method message() {
  2583. "Cannot have a 'whenever' block outside the scope of a 'supply' or 'react' block"
  2584. }
  2585. }
  2586. my class X::IllegalOnFixedDimensionArray is Exception {
  2587. has $.operation;
  2588. method message() {
  2589. "Cannot $.operation a fixed-dimension array"
  2590. }
  2591. }
  2592. my class X::NotEnoughDimensions is Exception {
  2593. has $.operation;
  2594. has $.got-dimensions;
  2595. has $.needed-dimensions;
  2596. method message() {
  2597. "Cannot $.operation a $.needed-dimensions dimension array with only $.got-dimensions dimensions"
  2598. }
  2599. }
  2600. my class X::TooManyDimensions is Exception {
  2601. has $.operation;
  2602. has $.got-dimensions;
  2603. has $.needed-dimensions;
  2604. method message() {
  2605. "Cannot $.operation a $.needed-dimensions dimension array with $.got-dimensions dimensions"
  2606. }
  2607. }
  2608. my class X::IllegalDimensionInShape is Exception {
  2609. has $.dim;
  2610. method message() {
  2611. "Illegal dimension in shape: $.dim. All dimensions must be integers bigger than 0"
  2612. }
  2613. }
  2614. my class X::Assignment::ArrayShapeMismatch is Exception {
  2615. has $.target-shape;
  2616. has $.source-shape;
  2617. method message() {
  2618. "Cannot assign an array of shape $.source-shape to an array of shape $.target-shape"
  2619. }
  2620. }
  2621. my class X::Assignment::ToShaped is Exception {
  2622. has $.shape;
  2623. method message() {
  2624. "Assignment to array with shape $.shape must provide structured data"
  2625. }
  2626. }
  2627. my class X::Language::Unsupported is Exception {
  2628. has $.version;
  2629. method message() {
  2630. "No compiler available for Perl $.version"
  2631. }
  2632. }
  2633. my class X::Proc::Unsuccessful is Exception {
  2634. has $.proc;
  2635. method message() {
  2636. "The spawned command '{$.proc.command[0]}' exited unsuccessfully (exit code: $.proc.exitcode())"
  2637. }
  2638. }
  2639. class CompUnit::DependencySpecification { ... }
  2640. my class X::CompUnit::UnsatisfiedDependency is Exception {
  2641. has CompUnit::DependencySpecification $.specification;
  2642. my sub is-core($name) {
  2643. my @parts = $name.split("::");
  2644. my $ns := ::CORE.WHO;
  2645. for @parts {
  2646. return False unless $ns{$_}:exists;
  2647. $ns := $ns{$_}.WHO;
  2648. };
  2649. True
  2650. };
  2651. method message() {
  2652. my $name = $.specification.short-name;
  2653. my $line = $.specification.source-line-number;
  2654. is-core($name)
  2655. ?? "{$name} is a builtin type, not an external module"
  2656. !! "Could not find $.specification at line $line in:\n"
  2657. ~ $*REPO.repo-chain.map(*.Str).join("\n").indent(4)
  2658. ~ ($.specification ~~ / $<name>=.+ '::from' $ /
  2659. ?? "\n\nIf you meant to use the :from adverb, use"
  2660. ~ " a single colon for it: $<name>:from<...>\n"
  2661. !! ''
  2662. )
  2663. }
  2664. }
  2665. my class Exceptions::JSON {
  2666. method process($ex) {
  2667. $*ERR.print: Rakudo::Internals::JSON.to-json($ex);
  2668. False # done processing
  2669. }
  2670. }