Sfoglia il codice sorgente

Fix range of "has no field" error (#8535)

* Fix range of "has no field" error

* Update positions in misc tests

* Fix position for 3714

* use non-physical equality

* adjust field position calc
Rudy Ges 6 anni fa
parent
commit
ee4f2c62d1

+ 14 - 13
src/typing/fields.ml

@@ -352,6 +352,7 @@ let rec using_field ctx mode e i p =
 
 (* Resolves field [i] on typed expression [e] using the given [mode]. *)
 let rec type_field cfg ctx e i p mode =
+	let pfield = if (e.epos = p) then p else {p with pmin = p.pmax - (String.length i)} in
 	let no_field() =
 		if TypeFieldConfig.do_resume cfg then raise Not_found;
 		let t = match follow e.etype with
@@ -370,9 +371,9 @@ let rec type_field cfg ctx e i p mode =
 			match t with
 			| TAbstract(a,_) when has_special_field a ->
 				(* the abstract field is not part of the field list, which is only true when it has no expression (issue #2344) *)
-				display_error ctx ("Field " ^ i ^ " cannot be called directly because it has no expression") p;
+				display_error ctx ("Field " ^ i ^ " cannot be called directly because it has no expression") pfield;
 			| _ ->
-				display_error ctx (StringError.string_error i (string_source t) (s_type (print_context()) t ^ " has no field " ^ i)) p;
+				display_error ctx (StringError.string_error i (string_source t) (s_type (print_context()) t ^ " has no field " ^ i)) pfield;
 		end;
 		AKExpr (mk (TField (e,FDynamic i)) (mk_mono()) p)
 	in
@@ -424,7 +425,7 @@ let rec type_field cfg ctx e i p mode =
 				| MCall,Var {v_read = AccCall } ->
 					()
 				| MCall, Var _ ->
-					display_error ctx "Cannot access superclass variable for calling: needs to be a proper method" p
+					display_error ctx "Cannot access superclass variable for calling: needs to be a proper method" pfield
 				| MCall, _ ->
 					()
 				| MGet,Var _
@@ -433,8 +434,8 @@ let rec type_field cfg ctx e i p mode =
 				| _, Method _ ->
 					display_error ctx "Cannot create closure on super method" p
 				| _ ->
-					display_error ctx "Normal variables cannot be accessed with 'super', use 'this' instead" p);
-			if not (can_access ctx c f false) && not ctx.untyped then display_error ctx ("Cannot access private field " ^ i) p;
+					display_error ctx "Normal variables cannot be accessed with 'super', use 'this' instead" pfield);
+			if not (can_access ctx c f false) && not ctx.untyped then display_error ctx ("Cannot access private field " ^ i) pfield;
 			field_access ctx mode f (match c2 with None -> FAnon f | Some (c,tl) -> FInstance (c,tl,f)) (apply_params c.cl_params params t) e p
 		with Not_found -> try
 			begin match e.eexpr with
@@ -464,7 +465,7 @@ let rec type_field cfg ctx e i p mode =
 					raise Not_found
 			end
 		with Not_found ->
-			if PMap.mem i c.cl_statics then error ("Cannot access static field " ^ i ^ " from a class instance") p;
+			if PMap.mem i c.cl_statics then error ("Cannot access static field " ^ i ^ " from a class instance") pfield;
 			no_field())
 	| TDynamic t ->
 		(try
@@ -474,12 +475,12 @@ let rec type_field cfg ctx e i p mode =
 	| TAnon a ->
 		(try
 			let f = PMap.find i a.a_fields in
-			if Meta.has Meta.Impl f.cf_meta && not (Meta.has Meta.Enum f.cf_meta) then display_error ctx "Cannot access non-static abstract field statically" p;
+			if Meta.has Meta.Impl f.cf_meta && not (Meta.has Meta.Enum f.cf_meta) then display_error ctx "Cannot access non-static abstract field statically" pfield;
 			if not (has_class_field_flag f CfPublic) && not ctx.untyped then begin
 				match !(a.a_status) with
 				| Closed | Extend _ -> () (* always allow anon private fields access *)
 				| Statics c when can_access ctx c f true -> ()
-				| _ -> display_error ctx ("Cannot access private field " ^ i) p
+				| _ -> display_error ctx ("Cannot access private field " ^ i) pfield
 			end;
 			let fmode, ft = (match !(a.a_status) with
 				| Statics c -> FStatic (c,f), field_type ctx c [] f p
@@ -532,7 +533,7 @@ let rec type_field cfg ctx e i p mode =
 		(try
 			let c = (match a.a_impl with None -> raise Not_found | Some c -> c) in
 			let f = PMap.find i c.cl_statics in
-			if not (can_access ctx c f true) && not ctx.untyped then display_error ctx ("Cannot access private field " ^ i) p;
+			if not (can_access ctx c f true) && not ctx.untyped then display_error ctx ("Cannot access private field " ^ i) pfield;
 			let field_type f =
 				if not (Meta.has Meta.Impl f.cf_meta) then begin
 					static_abstract_access_through_instance := true;
@@ -560,7 +561,7 @@ let rec type_field cfg ctx e i p mode =
 				AKNo f.cf_name
 			| (MGet | MCall), _ ->
 				let rec loop cfl = match cfl with
-					| [] -> error (Printf.sprintf "Field %s cannot be called on %s" f.cf_name (s_type (print_context()) e.etype)) p
+					| [] -> error (Printf.sprintf "Field %s cannot be called on %s" f.cf_name (s_type (print_context()) e.etype)) pfield
 					| cf :: cfl ->
 						match follow (apply_params a.a_params pl (monomorphs cf.cf_params cf.cf_type)) with
 							| TFun((_,_,t1) :: _,_) when type_iseq t1 (Abstract.get_underlying_type a pl) ->
@@ -575,7 +576,7 @@ let rec type_field cfg ctx e i p mode =
 				let t = field_type f in
 				begin match follow t with
 					| TFun((_,_,t1) :: _,_) -> ()
-					| _ -> error ("Invalid call to static function " ^ i ^ " through abstract instance") p
+					| _ -> error ("Invalid call to static function " ^ i ^ " through abstract instance") pfield
 				end;
 				let ef = field_expr f t in
 				AKUsing (ef,c,f,e,false)
@@ -614,9 +615,9 @@ let rec type_field cfg ctx e i p mode =
 			if not (TypeFieldConfig.allow_resolve cfg) then raise Not_found;
 			get_resolve (mode = MSet)
 		with Not_found ->
-			if !static_abstract_access_through_instance then error ("Invalid call to static function " ^ i ^ " through abstract instance") p
+			if !static_abstract_access_through_instance then error ("Invalid call to static function " ^ i ^ " through abstract instance") pfield
 			else no_field())
 	| _ ->
 		try using_field ctx mode e i p with Not_found -> no_field()
 
-let type_field_default_cfg = type_field TypeFieldConfig.default
+let type_field_default_cfg = type_field TypeFieldConfig.default

+ 1 - 1
tests/misc/projects/Issue1138/compile3-fail.hxml.stderr

@@ -1 +1 @@
-src1/D.hx:3: characters 9-17 : Int has no field triple
+src1/D.hx:3: characters 11-17 : Int has no field triple

+ 1 - 1
tests/misc/projects/Issue3607/compile-fail.hxml.stderr

@@ -1 +1 @@
-Main.hx:16: characters 9-16 : Base has no field f
+Main.hx:16: characters 15-16 : Base has no field f

+ 1 - 1
tests/misc/projects/Issue4816/compile1-fail.hxml.stderr

@@ -1 +1 @@
-Main1.hx:4: characters 25-31 : { f : { name : String } } has no field name
+Main1.hx:4: characters 27-31 : { f : { name : String } } has no field name

+ 1 - 1
tests/misc/projects/Issue4904/compile-fail.hxml.stderr

@@ -1 +1 @@
-Main.hx:3: characters 3-17 : Cannot access private field f
+Main.hx:3: characters 16-17 : Cannot access private field f

+ 1 - 1
tests/misc/projects/Issue5888/compile-fail.hxml.stderr

@@ -1 +1 @@
-Main.hx:5: characters 3-8 : Int has no field bar
+Main.hx:5: characters 5-8 : Int has no field bar

+ 1 - 1
tests/misc/projects/Issue5924/compile-fail.hxml.stderr

@@ -1 +1 @@
-Main.hx:5: characters 9-14 : Int has no field bar
+Main.hx:5: characters 11-14 : Int has no field bar

+ 1 - 1
tests/misc/projects/Issue5940/compile-fail.hxml.stderr

@@ -1 +1 @@
-Main.hx:3: characters 15-24 : Cannot access non-static abstract field statically
+Main.hx:3: characters 21-24 : Cannot access non-static abstract field statically

+ 2 - 2
tests/misc/projects/Issue6525/compile-fail.hxml.stderr

@@ -1,2 +1,2 @@
-Main.hx:16: characters 3-16 : Cannot access private field testPack
-Main.hx:17: characters 3-16 : Cannot access private field testPack
+Main.hx:16: characters 8-16 : Cannot access private field testPack
+Main.hx:17: characters 8-16 : Cannot access private field testPack

+ 5 - 0
tests/misc/projects/Issue8534/Main.hx

@@ -0,0 +1,5 @@
+class Main {
+	static function main() {
+		["1", "2", "3"].map(Std.parseInt).joint("\n");
+	}
+}

+ 1 - 0
tests/misc/projects/Issue8534/compile-fail.hxml

@@ -0,0 +1 @@
+Main.hx

+ 1 - 0
tests/misc/projects/Issue8534/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:3: characters 37-42 : Array<Null<Int>> has no field joint (Suggestion: join)