You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.0 KiB
46 lines
1.0 KiB
/*
|
|
* Copyright (c) 2018-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
class GlobalTest {
|
|
public static int s = 0;
|
|
public static Foo foo;
|
|
|
|
class Foo {
|
|
|
|
int x = 0;
|
|
// modifies global var 's' hence impure
|
|
void set_bad() {
|
|
s = 10;
|
|
}
|
|
}
|
|
|
|
void incr(Foo foo, int i) {
|
|
foo.x += i;
|
|
}
|
|
|
|
// calls foo which modifies global var
|
|
void call_set_bad() {
|
|
Foo f = new Foo();
|
|
f.set_bad();
|
|
}
|
|
|
|
// foo is global which is modified by incr.
|
|
void global_mod_via_argument_passing_bad(int size, Foo f) {
|
|
for (int i = 0; i < size; i++) {
|
|
incr(foo, i);
|
|
}
|
|
}
|
|
|
|
// aliased_foo is aliasing a global and then is modified by incr.
|
|
void global_mod_via_argument_passing_bad_FP(int size, Foo f) {
|
|
Foo aliased_foo = foo; // Inferbo can't recognize aliasing here
|
|
// and assumes aliased_foo is in [-oo,+oo] not in foo
|
|
for (int i = 0; i < size; i++) {
|
|
incr(aliased_foo, i);
|
|
}
|
|
}
|
|
}
|