123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115openStduneexceptionAlready_reportedtypewho_is_responsible_for_the_error=|User|Developerletget_user_message=function|User_error.Emsg->(User,msg)|Code_error.Ee->letopenPp.Oin(Developer,User_message.make?loc:e.loc[Pp.tagUser_message.Style.Error(Pp.textf"Internal error, please report upstream including the contents \
of _build/log.");Pp.text"Description:";Pp.box~indent:2(Pp.verbatim" "++Dyn.pp(Code_error.to_dyn_without_loce))])|Unix.Unix_error(err,func,fname)->letopenPp.Oin(User,User_error.make[User_error.prefix++Pp.textf" %s: %s: %s"funcfname(Unix.error_messageerr)])|exn->letopenPp.Oinlets=Printexc.to_stringexninletloc,pp=matchScanf.sscanfs"File %S, line %d, characters %d-%d:"(funabcd->(a,b,c,d))with|Error()->(None,User_error.prefix++Pp.textf" exception %s"s)|Ok(fname,line,start,stop)->letstart:Lexing.position={pos_fname=fname;pos_lnum=line;pos_cnum=start;pos_bol=0}inletstop={startwithpos_cnum=stop}in(Some{Loc.start;stop},Pp.texts)in(Developer,User_message.make?loc[pp])leti_must_not_crash=letreported=reffalseinfun()->if!reportedthen[]else(reported:=true;[Pp.nop;Pp.text"I must not crash. Uncertainty is the mind-killer. Exceptions are \
the little-death that brings total obliteration. I will fully \
express my cases. Execution will pass over me and through me. And \
when it has gone past, I will unwind the stack along its path. \
Where the cases are handled there will be nothing. Only I will \
remain."])letreported=refDigest.Set.emptyletreport_backtraces_flag=reffalseletreport_backtracesb=report_backtraces_flag:=bletclear_reported()=reported:=Digest.Set.emptyletbuf=Buffer.create128letppf=Format.formatter_of_bufferbufletreport?(extra=fun_->None){Exn_with_backtrace.exn;backtrace}=matchexnwith|Already_reported->()|_->letwho_is_responsible,msg=get_user_messageexninletmsg=ifmsg.loc=SomeLoc.nonethen{msgwithloc=None}elsemsginlethash=Digest.genericmsginifnot(Digest.Set.mem!reportedhash)then(reported:=Digest.Set.add!reportedhash;letappend(msg:User_message.t)pp={msgwithparagraphs=msg.paragraphs@pp}inletmsg=ifwho_is_responsible=User&¬!report_backtraces_flagthenmsgelseappendmsg(List.map(Printexc.raw_backtrace_to_stringbacktrace|>String.split_lines)~f:(funline->Pp.box~indent:2(Pp.textline)))inletmsg=matchextramsg.locwith|None->msg|Somepp->appendmsg[pp]inletmsg=matchwho_is_responsiblewith|User->msg|Developer->appendmsg(i_must_not_crash())inConsole.print_user_messagemsg)