Closure Compiler - warning: restricted index type found:string, required:number - google-closure-compiler

I get this warning:
WARNING - restricted index type
found : string
required: number
someArray[ index ].doSomething();
This happens after a closure compiler upgrade to the latest version.
It looks like the use of a string type indexes for arrays are not recommended by closure compiler.
What would be the recommended solution to this problem?
BTW. Is there a way to disable check for these warning types (I looked through the CC flags list and can't find anything)?

If your index variable is of type string, you should parse it first.
Try
someArray[parseInt(index)].doSomething();
Additionally, I assume that the reason it's a string in the first place is that it comes from somewhere like a DOM attribute or an HTML input. You might want to make sure the value is valid, before using it.
const parsedIndex = parseInt(index);
if (isNaN(parsedIndex) || index < 0) {
throw 'Invalid index';
}
someArray[parsedIndex].doSomething();

Related

How and why is a GNAT.Strings.String_List use clause disallowed? How can you use System.Strings.String_List."&" with infix notation?

Posting for two reasons: (1) I was stuck on unhelpful compiler errors for far too long for such a simple issue and I want the next person to google those messages to come upon my (or other) answers, and (2) I still don't understand disallowing a use clause, so my own answer is really incomplete.
In order to call a program in two places with mostly the same arguments, I want to use the '&' to append to a default list inline:
declare
Exit_Code : constant Integer := GNAT.OS_Lib.Spawn (Program_Name => "gprbuild", Args => (Default_GPR_Arguments & new String'(File_Name_Parameter)));
begin
if Exit_Code /= 0 then
raise Program_Error with "Exit code:" & Exit_Code'Image;
end if;
end;
However, the compiler complains that System.Strings.String_List needs a use clause:
operator for type "System.Strings.String_List" is not directly visible
use clause would make operation legal
But inserting use System.Strings.String_List yields:
"System.Strings.String_List" is not allowed in a use clause
I also got this warning:
warning: "System.Strings" is an internal GNAT unit
warning: use "GNAT.Strings" instead
So I substituted GNAT for System in the with and the use clause and got an extra error in addition to the original 'you need a use clause for System.Strings.String_List' one:
"GNAT.Strings.String_List" is not allowed in a use clause
Why is GNAT.Strings.String_List not allowed in a use clause? Section 8.5 on use clauses doesn't seem to state anything on disallowed packages, so is this a compiler bug? Is it possible to define a new package that cannot have a use clause?
In a use clause of the form
use Name;
Name must be a package name. GNAT.Strings.String_List is a subtype name, not a package name.
There are a number of ways to invoke "&" for String_List. The simplest is to use the full name:
GNAT.Strings."&" (Left, Right)
but presumably you want to be able to use it as an operator in infix notation, Left & Right. Ways to achieve this, in decreasing specificity:
function "&" (Left : GNAT.Strings.String_List; Right : GNAT.Strings.String_List) return GNAT.Strings.String_List renames GNAT.Strings."&"; This makes this specific function directly visible.
use type GNAT.Strings.String_List; This makes all primitive operators of the type directly visible.
use all type GNAT.Strings.String_List; This makes all primitive operations of the type (including non-operator operations) directly visible.
use GNAT.Strings; This makes everything in the package directly visible.
Looks like it is a design decision. And many other packages in System follows this rule. From the s-string.ads (package specification for System.String):
-- Note: this package is in the System hierarchy so that it can be directly
-- be used by other predefined packages. User access to this package is via
-- a renaming of this package in GNAT.String (file g-string.ads).
My guess why this is done in that way: because it isn't in the Ada specification, but extension from GNAT.

How to get the value of system()?

How do I get the current value of system() in premake5?
(and more generally functions like architecture() or platform())
I tried to print it, but it's a function, and when I try to print the return value of system(), I get "bad argument #1 to 'tostring' (value expected)".
Premake doesn't work that way, there is no "current" version of the data. You have to specify the context in which you'd like the current set of filters to be applied; take a look at src/base/oven.lua to see how the final data sets are built.
If you just want to drop the value of system (or architecture or platform) into an expression later in the process (and you're using Premake 5), check out tokens:
targetdir "bin/%{cfg.system}/%{cfg.architecture}"
Tokens can also evaluate arbitrary Lua expressions.
my_system_map = { -- must be global, so token evaluator can find it
windows = "Win32",
linux = "Posix",
macosx = "Mac"
}
targetdir "bin/%{my_system_map[cfg.system]}"
Helpful?

LLVM converting a Constant to a Value

I am using custom LLVM pass where if I encounter a store to
where the compiler converts the value to a Constant; e.g. there is an explicit store:
X[gidx] = 10;
Then LLVM will generate this error:
aoc: ../../../Instructions.cpp:1056: void llvm::StoreInst::AssertOK(): Assertion `getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"' failed.
The inheritance order goes as: Value<-User<-Constant, so this shouldn't be an issue, but it is. Using an a cast on the ConstantInt or ConstantFP has no effect on this error.
So I've tried this bloated solution:
Value *new_value;
if(isa<ConstantInt>(old_value) || isa<ConstantFP>(old_value)){
Instruction *allocInst = builder.CreateAlloca(old_value->getType());
builder.CreateStore(old_value, allocInst);
new_value = builder.CreateLoad(allocResultInst);
}
However this solution creates its own register errors when different type are involved, so I'd like to avoid it.
Does anyone know how to convert a Constant to a Value? It must be a simple issue that I'm not seeing. I'm developing on Ubuntu 12.04, LLVM 3, AMD gpu, OpenCL kernels.
Thanks ahead of time.
EDIT:
The original code that produces the first error listed is simply:
builder.CreateStore(old_value, store_addr);
EDIT2:
This old_value is declared as
Value *old_value = current_instruction->getOperand(0);
So I'm grabbing the value to be stored, in this case "10" from the first code line.
You didn't provide the code that caused this first assertion, but its wording is pretty clear: you are trying to create a store where the value operand and the pointer operand do not agree on their types. It would be useful for the question if you'd provide the code that generated that error.
Your second, so-called "bloated" solution, is the correct way to store old_value into the stack and then load it again. You write:
However this solution creates its own register errors when different type are involved
These "register errors" are the real issue you should be addressing.
In any case, the whole premise of "converting a constant to a value" is flawed - as you have correctly observed, all constants are values. There's no point storing a value into the stack with the sole purpose of loading it again, and indeed the standard LLVM pass "mem2reg" will completely remove such a sequence, replacing all uses of the load with the original value.

what is needed in order to use db.scan ()

I'm trying to get the scan function to work, but it seems i'm missing something: According to the documention this should work:
var iter = new ydn.db.KeyIterator('contact');
db.scan([iter], function(keys, values)
But every time i call the function i get the following error:
Uncaught ydn.error.ArgumentException: Iterator argument must be an array.
A contact sore with the name 'contact' exists and i tried different libraries ydb-db but none of them worked.
Documentation is outdated. It should be:
db.scan(function(keys, values), [iter]
See unit test for it use case.

unitended reference comparision

I am getting this warning:
Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'
I tried this:
if (Convert.ToString(Session["message"]) == SESSIONTIMEOUT)
or
if (Session["message"].ToString() == SESSIONTIMEOUT)
But I'm still getting the above message.
You should use the Equals method for comparison of Strings, like this:
if (Session["message"].ToString().Equals(SESSIONTIMEOUT))
Generally speaking, the == operator is supposed to perform identity comparison - i.e., verifying that two object references point at the same object. See http://www.andymcm.com/csharpfaq.htm#6.7 for more information.
If you read the compiler warning message carefully, it says how you should solve it:
if ((string)Session["message"] == SESSIONTIMEOUT)
That's what casting the left hand side to string means, and this should be the solution. The reason why the message didn't go away, is that you didn't perform a complete rebuild (recompilation) of your project.
Now if there is any change the left hand side could be an object that's not a string, then use:
if (Session["message"] as string == SESSIONTIMEOUT)
but I guess you will not allow the type of your message to be non-string, so go for my first solution, the one suggested by the warning message.

Resources