Skip to content

Commit 1ac2f3c

Browse files
authored
Do not fail in case of moved file (#1439)
Commit to mitigate Issue #1436 lambdapi install moves .lp and .lpo files into the standard lib_root; moreover when "lambdapi build" is called from opam, the files are also moved to yet another location (inside the sandbox) to compile them. However, the .lpo are NOT relocatable in the sense that filenames inside positions `pos` are absolute path. We use them: a) during indexing of rules to build a `qualid` for the rule b) to open the file to show the result of queries Both use cases (a and b) used to fail. In particular, a) prevented to import a library from the standard `lib_root`. As a mitigation, we replace the hard failures with degradation of functionality. Further work will be necessary to restore the full functionality. In particular, libraries installed in stdlib_root should always be globally indexed.
1 parent 701cf69 commit 1ac2f3c

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/common/pos.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ let print_file_contents :
209209
| Some { fname=Some fname; start_line; start_col; end_line; end_col } ->
210210
(* WARNING: do not try to understand the following code!
211211
It's dangerous for your health! *)
212-
213-
let input_line,finish = parse_file fname in
212+
begin match parse_file fname with
213+
| exception _ ->
214+
string ppf ("Cannot open file \""^fname^"\". Maybe it was moved?")
215+
| input_line,finish ->
214216
let out =
215217
Buffer.create
216218
((end_line - start_line) * 80 +
@@ -273,6 +275,7 @@ let print_file_contents :
273275

274276
finish () ;
275277
string ppf (Buffer.contents out)
278+
end
276279
| None | Some {fname=None} ->
277280
if complain_if_location_unknown then
278281
string ppf (db ^ "unknown location" ^ de)

src/core/sign.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ let unlink : t -> unit = fun sign ->
228228

229229
(** [add_symbol_callback] is used to index symbols in the document currently
230230
edited in Spl mode*)
231-
let add_symbol_callback = Stdlib.ref (fun _ -> ())
231+
let add_symbol_callback = Stdlib.ref (fun ~path:_ _ -> ())
232232

233233
(** [add_rules_callback] is used to index rules in the document currently
234234
edited in Spl mode*)
235-
let add_rules_callback = Stdlib.ref (fun _ _ -> ())
235+
let add_rules_callback = Stdlib.ref (fun ~path:_ _ _ -> ())
236236

237237
(** [add_symbol sign expo prop mstrat opaq name pos typ impl notation] adds in
238238
the signature [sign] a symbol with name [name], exposition [expo],
@@ -248,7 +248,8 @@ let add_symbol : t -> expo -> prop -> match_strat -> bool -> strloc ->
248248
(cleanup typ) (minimize_impl impl)
249249
in
250250
sign.sign_symbols := StrMap.add name.elt sym !(sign.sign_symbols);
251-
if Stdlib.(!Common.Console.lsp_mod) then Stdlib.(!add_symbol_callback sym) ;
251+
if Stdlib.(!Common.Console.lsp_mod) then
252+
Stdlib.(!add_symbol_callback ~path:sign.sign_path sym) ;
252253
sym
253254

254255
(** [strip_private sign] removes private symbols from signature [sign]. *)
@@ -362,7 +363,8 @@ let add_rules : t -> sym -> rule list -> unit = fun sign s rs ->
362363
let d = {d with dep_symbols=sm} in
363364
sign.sign_deps := Path.Map.add s.sym_path d !(sign.sign_deps)
364365
end ;
365-
if Stdlib.(!Common.Console.lsp_mod) then Stdlib.(!add_rules_callback s rs)
366+
if Stdlib.(!Common.Console.lsp_mod) then
367+
Stdlib.(!add_rules_callback ~path:sign.sign_path s rs)
366368

367369
(** [add_rule sign s r] adds the new rule [r] to the symbol [s]. When the rule
368370
does not correspond to a symbol of signature [sign], it is stored in its

src/tool/indexing.ml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ let index_term_and_subterms ~is_spine t item =
731731
List.sort_uniq cmp (subterms_to_index ~is_spine tn) in
732732
List.iter (fun (where,s) -> insert_rigid s (item where)) subterms
733733

734-
let index_rule sym ({Core.Term.lhs=lhsargs ; rule_pos ; _} as rule) =
734+
let index_rule ~path sym ({Core.Term.lhs=lhsargs ; rule_pos ; _} as rule) =
735735
let rule_pos =
736736
match rule_pos with
737737
| None -> assert false (* this probably may happen, but it is BAD!
@@ -741,9 +741,6 @@ let index_rule sym ({Core.Term.lhs=lhsargs ; rule_pos ; _} as rule) =
741741
let lhs = Core.Term.add_args (Core.Term.mk_Symb sym) lhsargs in
742742
let rhs = rule.rhs in
743743
let get_relation = function | DB.Conclusion r -> r | _ -> assert false in
744-
let filename = Option.get rule_pos.fname in
745-
let filename = Lplib.String.remove_prefix "file://" filename in
746-
let path = Library.path_of_file Parsing.LpLexer.escape filename in
747744
let rule_name = (path,Common.Pos.to_string ~print_fname:false rule_pos) in
748745
index_term_and_subterms ~is_spine:false lhs
749746
(fun where -> ((rule_name,Some rule_pos),[Xhs(get_relation where,Lhs)])) ;
@@ -752,9 +749,9 @@ let index_rule sym ({Core.Term.lhs=lhsargs ; rule_pos ; _} as rule) =
752749

753750
let _ =
754751
Stdlib.(Core.Sign.add_rules_callback :=
755-
fun sym rules -> List.iter (index_rule sym) rules)
752+
fun ~path sym rules -> List.iter (index_rule ~path sym) rules)
756753

757-
let index_sym sym =
754+
let index_sym ~path sym =
758755
let qname = name_of_sym sym in
759756
(* Name *)
760757
if List.exists (fun ((sn,_),_) -> sn=qname)
@@ -771,7 +768,7 @@ let index_sym sym =
771768
(* InBody??? sym.sym_def : term option ref
772769
but all the subterms are too much; collect only the constants? *)
773770
(* Rules *)
774-
List.iter (index_rule sym) Timed.(!(sym.Core.Term.sym_rules))
771+
List.iter (index_rule ~path sym) Timed.(!(sym.Core.Term.sym_rules))
775772

776773
let _ = Stdlib.(Core.Sign.add_symbol_callback := index_sym)
777774

@@ -784,13 +781,14 @@ let index_sign sign =
784781
Common.Logger.set_debug true "e" ;*)
785782
let syms = Timed.(!(sign.Core.Sign.sign_symbols)) in
786783
let deps = Timed.(!(sign.Core.Sign.sign_deps)) in
787-
Lplib.Extra.StrMap.iter (fun _ sym -> index_sym sym) syms ;
784+
let path = sign.Core.Sign.sign_path in
785+
Lplib.Extra.StrMap.iter (fun _ sym -> index_sym ~path sym) syms ;
788786
Common.Path.Map.iter
789-
(fun path d ->
787+
(fun p d ->
790788
Lplib.Extra.StrMap.iter
791789
(fun name sd ->
792-
let sym = Core.Sign.find_qualified path name in
793-
List.iter (index_rule sym) sd.Sign.rules)
790+
let sym = Core.Sign.find_qualified p name in
791+
List.iter (index_rule ~path sym) sd.Sign.rules)
794792
d.Sign.dep_symbols)
795793
deps
796794

0 commit comments

Comments
 (0)