Browse Source

[dce] use zero-range positions for @:keep and @:used metadata (closes #8948, closes #8992) (#8995)

Dan Korostelev 5 years ago
parent
commit
ea8b45b248
4 changed files with 30 additions and 8 deletions
  1. 2 0
      src/core/globals.ml
  2. 1 1
      src/filters/filters.ml
  3. 13 7
      src/optimization/dce.ml
  4. 14 0
      tests/server/src/DisplayTests.hx

+ 2 - 0
src/core/globals.ml

@@ -71,6 +71,8 @@ let platform_list_help = function
 
 
 let null_pos = { pfile = "?"; pmin = -1; pmax = -1 }
 let null_pos = { pfile = "?"; pmin = -1; pmax = -1 }
 
 
+let mk_zero_range_pos p = { p with pmax = p.pmin }
+
 let s_type_path (p,s) = match p with [] -> s | _ -> String.concat "." p ^ "." ^ s
 let s_type_path (p,s) = match p with [] -> s | _ -> String.concat "." p ^ "." ^ s
 
 
 let starts_with s c =
 let starts_with s c =

+ 1 - 1
src/filters/filters.ml

@@ -671,7 +671,7 @@ let check_cs_events com t = match t with
 
 
 					(* add @:keep to event methods if the event is kept *)
 					(* add @:keep to event methods if the event is kept *)
 					if Meta.has Meta.Keep f.cf_meta && not (Meta.has Meta.Keep m.cf_meta) then
 					if Meta.has Meta.Keep f.cf_meta && not (Meta.has Meta.Keep m.cf_meta) then
-						m.cf_meta <- (Meta.Keep,[],f.cf_pos) :: m.cf_meta;
+						m.cf_meta <- (Dce.mk_keep_meta f.cf_pos) :: m.cf_meta;
 				in
 				in
 				process_event_method ("add_" ^ f.cf_name);
 				process_event_method ("add_" ^ f.cf_name);
 				process_event_method ("remove_" ^ f.cf_name)
 				process_event_method ("remove_" ^ f.cf_name)

+ 13 - 7
src/optimization/dce.ml

@@ -97,6 +97,12 @@ let keep_whole_enum dce en =
 	Meta.has_one_of keep_metas en.e_meta
 	Meta.has_one_of keep_metas en.e_meta
 	|| not (dce.full || is_std_file dce en.e_module.m_extra.m_file || has_meta Meta.Dce en.e_meta)
 	|| not (dce.full || is_std_file dce en.e_module.m_extra.m_file || has_meta Meta.Dce en.e_meta)
 
 
+let mk_used_meta pos =
+	Meta.Used,[],(mk_zero_range_pos pos)
+
+let mk_keep_meta pos =
+	Meta.Keep,[],(mk_zero_range_pos pos)
+
 (*
 (*
 	Check if a field is kept.
 	Check if a field is kept.
 	`keep_field` is checked to determine the DCE entry points, i.e. all fields that have `@:keep` or kept for other reasons.
 	`keep_field` is checked to determine the DCE entry points, i.e. all fields that have `@:keep` or kept for other reasons.
@@ -148,7 +154,7 @@ and check_and_add_feature dce s =
 and mark_field dce c cf stat =
 and mark_field dce c cf stat =
 	let add cf =
 	let add cf =
 		if not (Meta.has Meta.Used cf.cf_meta) then begin
 		if not (Meta.has Meta.Used cf.cf_meta) then begin
-			cf.cf_meta <- (Meta.Used,[],cf.cf_pos) :: cf.cf_meta;
+			cf.cf_meta <- (mk_used_meta cf.cf_pos) :: cf.cf_meta;
 			dce.added_fields <- (c,cf,stat) :: dce.added_fields;
 			dce.added_fields <- (c,cf,stat) :: dce.added_fields;
 			dce.marked_fields <- cf :: dce.marked_fields;
 			dce.marked_fields <- cf :: dce.marked_fields;
 			check_feature dce (Printf.sprintf "%s.%s" (s_type_path c.cl_path) cf.cf_name);
 			check_feature dce (Printf.sprintf "%s.%s" (s_type_path c.cl_path) cf.cf_name);
@@ -195,13 +201,13 @@ let rec update_marked_class_fields dce c =
 
 
 (* mark a class as kept. If the class has fields marked as @:?keep, make sure to keep them *)
 (* mark a class as kept. If the class has fields marked as @:?keep, make sure to keep them *)
 and mark_class dce c = if not (Meta.has Meta.Used c.cl_meta) then begin
 and mark_class dce c = if not (Meta.has Meta.Used c.cl_meta) then begin
-	c.cl_meta <- (Meta.Used,[],c.cl_pos) :: c.cl_meta;
+	c.cl_meta <- (mk_used_meta c.cl_pos) :: c.cl_meta;
 	check_feature dce (Printf.sprintf "%s.*" (s_type_path c.cl_path));
 	check_feature dce (Printf.sprintf "%s.*" (s_type_path c.cl_path));
 	update_marked_class_fields dce c;
 	update_marked_class_fields dce c;
 end
 end
 
 
 let rec mark_enum dce e = if not (Meta.has Meta.Used e.e_meta) then begin
 let rec mark_enum dce e = if not (Meta.has Meta.Used e.e_meta) then begin
-	e.e_meta <- (Meta.Used,[],e.e_pos) :: e.e_meta;
+	e.e_meta <- (mk_used_meta e.e_pos) :: e.e_meta;
 	check_and_add_feature dce "has_enum";
 	check_and_add_feature dce "has_enum";
 	check_feature dce (Printf.sprintf "%s.*" (s_type_path e.e_path));
 	check_feature dce (Printf.sprintf "%s.*" (s_type_path e.e_path));
 	PMap.iter (fun _ ef -> mark_t dce ef.ef_pos ef.ef_type) e.e_constrs;
 	PMap.iter (fun _ ef -> mark_t dce ef.ef_pos ef.ef_type) e.e_constrs;
@@ -209,7 +215,7 @@ end
 
 
 and mark_abstract dce a = if not (Meta.has Meta.Used a.a_meta) then begin
 and mark_abstract dce a = if not (Meta.has Meta.Used a.a_meta) then begin
 	check_feature dce (Printf.sprintf "%s.*" (s_type_path a.a_path));
 	check_feature dce (Printf.sprintf "%s.*" (s_type_path a.a_path));
-	a.a_meta <- (Meta.Used,[],a.a_pos) :: a.a_meta
+	a.a_meta <- (mk_used_meta a.a_pos) :: a.a_meta
 end
 end
 
 
 (* mark a type as kept *)
 (* mark a type as kept *)
@@ -219,7 +225,7 @@ and mark_t dce p t =
 		begin match follow t with
 		begin match follow t with
 		| TInst({cl_kind = KTypeParameter tl} as c,pl) ->
 		| TInst({cl_kind = KTypeParameter tl} as c,pl) ->
 			if not (Meta.has Meta.Used c.cl_meta) then begin
 			if not (Meta.has Meta.Used c.cl_meta) then begin
-				c.cl_meta <- (Meta.Used,[],c.cl_pos) :: c.cl_meta;
+				c.cl_meta <- (mk_used_meta c.cl_pos) :: c.cl_meta;
 				List.iter (mark_t dce p) tl;
 				List.iter (mark_t dce p) tl;
 			end;
 			end;
 			List.iter (mark_t dce p) pl
 			List.iter (mark_t dce p) pl
@@ -779,7 +785,7 @@ let sweep dce com =
 				end;
 				end;
 			in
 			in
 			(* add :keep so subsequent filter calls do not process class fields again *)
 			(* add :keep so subsequent filter calls do not process class fields again *)
-			c.cl_meta <- (Meta.Keep,[],c.cl_pos) :: c.cl_meta;
+			c.cl_meta <- (mk_keep_meta c.cl_pos) :: c.cl_meta;
  			c.cl_ordered_statics <- List.filter (fun cf ->
  			c.cl_ordered_statics <- List.filter (fun cf ->
 				let b = keep_field dce cf c true in
 				let b = keep_field dce cf c true in
 				if not b then begin
 				if not b then begin
@@ -843,7 +849,7 @@ let run com main mode =
 	} in
 	} in
 	begin match main with
 	begin match main with
 		| Some {eexpr = TCall({eexpr = TField(e,(FStatic(c,cf)))},_)} | Some {eexpr = TBlock ({ eexpr = TCall({eexpr = TField(e,(FStatic(c,cf)))},_)} :: _)} ->
 		| Some {eexpr = TCall({eexpr = TField(e,(FStatic(c,cf)))},_)} | Some {eexpr = TBlock ({ eexpr = TCall({eexpr = TField(e,(FStatic(c,cf)))},_)} :: _)} ->
-			cf.cf_meta <- (Meta.Keep,[],cf.cf_pos) :: cf.cf_meta
+			cf.cf_meta <- (mk_keep_meta cf.cf_pos) :: cf.cf_meta
 		| _ ->
 		| _ ->
 			()
 			()
 	end;
 	end;

+ 14 - 0
tests/server/src/DisplayTests.hx

@@ -245,4 +245,18 @@ typedef Foo = {
 			case _: false;
 			case _: false;
 		});
 		});
 	}
 	}
+
+	function testIssue8992() {
+		var mainHx = Marker.extractMarkers('class Main {
+	static func{-1-}tion main() {
+	}
+}');
+		vfs.putContent("Main.hx", mainHx.source);
+
+		runHaxe(["--no-output", "-main", "Main"]);
+		runHaxeJson([], DisplayMethods.Hover, {file: new FsPath("Main.hx"), offset: mainHx.markers[1]});
+
+		var result = parseHover().result;
+		Assert.isNull(result);
+	}
 }
 }