From a3eb06d5b2b2dfd7d10bc1b3a44fc654d3cdf062 Mon Sep 17 00:00:00 2001 From: Kathy Gray Date: Wed, 26 Apr 2006 17:33:37 +0000 Subject: [PATCH] Corrected error of unreachable return in if without else and no return. svn: r2805 --- collects/profj/check.ss | 3 +- collects/tests/profj/advanced-tests.ss | 51 ++++++--- collects/tests/profj/beginner-tests.ss | 6 +- collects/tests/profj/full-tests.ss | 126 ++++++++++----------- collects/tests/profj/intermediate-tests.ss | 10 +- 5 files changed, 110 insertions(+), 86 deletions(-) diff --git a/collects/profj/check.ss b/collects/profj/check.ss index f6461bd3bf..48f0629959 100644 --- a/collects/profj/check.ss +++ b/collects/profj/check.ss @@ -799,7 +799,8 @@ ((ifS? body) (if (ifS-else body) (and (reachable-return? (ifS-then body)) - (reachable-return? (ifS-else body))))) + (reachable-return? (ifS-else body))) + #f)) ((throw? body) #t) ((return? body) #t) ((while? body) (reachable-return? (while-loop body))) diff --git a/collects/tests/profj/advanced-tests.ss b/collects/tests/profj/advanced-tests.ss index 3f7c2ceb77..d38d1da953 100644 --- a/collects/tests/profj/advanced-tests.ss +++ b/collects/tests/profj/advanced-tests.ss @@ -22,13 +22,13 @@ 'advanced #f "while loop with statements after") (execute-test - "interface A { int a( int x); } - abstract class B implements A { } + "interface Abs { int a( int x); } + abstract class Babs implements Abs { } " 'advanced #f "abs. class with interface not all impl., with args") (execute-test - "public class X { + "public class Xprivate { private int x() { return 3; } }" 'advanced #f "Class with private method") @@ -83,17 +83,27 @@ 'advanced #f "Class containing inits") (execute-test - "class X { + "class Xfinal { final int x() { return 4; } }" 'advanced #f "Class with final method") (execute-test - "class X { + "class Xoverload { int x() { return 3; } int x( int y ) { return y; } }" 'advanced #f "Class with overloaded methods") + (execute-test + "class Ret { + boolean rets() { + if (true) + return true; + return false; + } + }" + 'advanced #f "If with no else, reachable return") + ;;Execution tests with errors (execute-test @@ -124,8 +134,8 @@ }" 'advanced #t "Attempt to set before named, init") (execute-test - "class X { X() { this(1); } - X(int i) { this(); }}" + "class Xth { Xth() { this(1); } + Xth(int i) { this(); }}" 'advanced #t "Cyclic calls to this") (execute-test @@ -248,36 +258,49 @@ class WeeklyPlanner{ } " 'advanced #t "Parsing test from student program. Should mention {") + (execute-test + "class NoRet { + boolean noRet() { + if (true) + return true; + } + }" + 'advanced #t "If with no else, and return in if") + ;;Interaction tests, mix of right and error (interact-test - "class A { + "class Afirst { private int x = 10; public int y = x * 2; } - class B { + class Bsecond { public int x = 10; private int y = x * 2; public int report() { return y; } } - class C { + class Cthird { public int x = 10; public int y = 2 * x; } - class D { + class Dfourth { int x = 10; public int y = 2 * x; }" 'advanced - '("A a = new A();" "B b = new B();" "C c = new C();" "D d = new D();" "a.y" "b.report()" "c.y" "d.y") + '("Afirst a = new Afirst();" + "Bsecond b = new Bsecond();" + "Cthird c = new Cthird();" + "Dfourth d = new Dfourth();" + "a.y" "b.report()" "c.y" "d.y") '((void) (void) (void) (void) 20 20 20 20) "Private, etc shouldn't effect order of evaluation") (interact-test - "public class X { + "public class Xp { private int x() { return 3; } }" - 'advanced '("X y = new X();" "y.x()") + 'advanced '("Xp y = new Xp();" "y.x()") '((void) error) "Creating class with private method, and trying to access the method") diff --git a/collects/tests/profj/beginner-tests.ss b/collects/tests/profj/beginner-tests.ss index 1a2f0e2faf..0266358a19 100644 --- a/collects/tests/profj/beginner-tests.ss +++ b/collects/tests/profj/beginner-tests.ss @@ -293,9 +293,9 @@ "Tests instantiating a class") (interact-test - "class Book { + "class Book2 { int numPages; - Book( int numPages ) { + Book2( int numPages ) { this.numPages = numPages; } String level() { @@ -309,7 +309,7 @@ } " language - (list "new Book(9).level()" "new Book(10).level()" "new Book(100).level()" "new Book(99).level") + (list "new Book2(9).level()" "new Book2(10).level()" "new Book2(100).level()" "new Book2(99).level") (list (make-java-string "Apprentice") (make-java-string "Journeyman") (make-java-string "Master") 'error) "Tests of an if in a method") diff --git a/collects/tests/profj/full-tests.ss b/collects/tests/profj/full-tests.ss index 225b4b93d7..32b9e301bd 100644 --- a/collects/tests/profj/full-tests.ss +++ b/collects/tests/profj/full-tests.ss @@ -5,44 +5,44 @@ (prepare-for-tests "Full") (execute-test - "class A { - int f (A x) { return 4; } + "class Aextendee { + int f (Aextendee x) { return 4; } } - class B extends A { - int f( B x) { return 5; } + class Bextendor extends Aextendee { + int f( Bextendor x) { return 5; } }" 'full #f "Overloading introduced on extends") (execute-test - "class X { + "class Xforward { int x = y; int y; }" 'full #t "Forward reference error") (interact-test - "class X { + "class Xnoundef { int x = this.y; int y = 2; }" 'full - '("new X().x" "new X().y") + '("new Xnoundef().x" "new Xnoundef().y") '(0 2) "Testing no undefined fields") (parameterize ((dynamic? #t)) (interact-test - "class X { }" + "class Xeq { }" 'full - '("X x = new X();" "X y = (dynamic) x;" "x.equals(y)" "y.equals(x)" "y==x" "x==y") + '("Xeq x = new Xeq();" "Xeq y = (dynamic) x;" "x.equals(y)" "y.equals(x)" "y==x" "x==y") '((void) (void) #t #t #t #t) "Equality test of a wrapped and unwrapped object")) (parameterize ((dynamic? #t)) (interact-test - "class X { int y; X(int y) { this.y = y; } }" + "class Xacc { int y; Xacc(int y) { this.y = y; } }" 'full - '("X x = new X(3);" "X y = (dynamic) x;" "x.y = 4" "y.y" "y.y=5" "x.y") + '("Xacc x = new Xacc(3);" "Xacc y = (dynamic) x;" "x.y = 4" "y.y" "y.y=5" "x.y") '((void) (void) 4 4 5 5) "Accessing fields of a dynamic value")) @@ -52,9 +52,9 @@ 'full #f "Statement inner class accessing package field") (parameterize ((dynamic? #t)) - (interact-test "class A { }" + (interact-test "class Acast { }" 'full - '("dynamic x = new A();" "A a = x;" "(A) a") + '("dynamic x = new Acast();" "Acast a = x;" "(Acast) a") '((void) (void) a~f) "Casting a guarded/asserted value back to the original type")) @@ -72,20 +72,20 @@ (parameterize ((dynamic? #t)) (interact-test - "class X{ int x( int i) { return i; }}" + "class Xcastd{ int x( int i) { return i; }}" 'full - '("((dynamic) new X()).x(1)" "((dynamic) new X()).x()") + '("((dynamic) new Xcastd()).x(1)" "((dynamic) new Xcastd()).x()") '(1 error) "Test of casting known values to dynamic")) (execute-test - "interface A {} - interface B {} - class C implements A, B { + "interface Aa {} + interface Ba {} + class Ca implements Aa, Ba { static void go() { - C c = new C(); - A a = c; - B b = c; + Ca c = new Ca(); + Aa a = c; + Ba b = c; if (a == b) { b=b; @@ -100,43 +100,43 @@ }" 'full #f "test of ==, using castable") (execute-test - "class A { } - class B extends A { } - class C extends A { } - class X { - A a = new B(); - C b = new C(); + "class Ab { } + class Bb extends Ab { } + class Cb extends Ab { } + class Xb { + Ab a = new Bb(); + Cb b = new Cb(); boolean e() { return a == b; } }" 'full #f "Test of ==") (execute-test - "class A { } - class B extends A { } - class C extends A { } - class X { - B a = new B(); - C b = new C(); + "class Ac { } + class Bc extends Ac { } + class Cc extends Ac { } + class Xc { + Bc a = new Bc(); + Cc b = new Cc(); boolean e() { return a == b; } }" 'full #t "Incompatible type test ==") (execute-test - "class A { + "class Ad { boolean b() { return \"hi\" == new Object(); } }" 'full #f "Comparing String and Object") (execute-test - "final class A { + "final class Ae { } - interface B { } - class X { - Object o( A a ) { - return (B) a; + interface Be { } + class Xe { + Object o( Ae a ) { + return (Be) a; } }" 'full #t "Cast from final class to unimpl interface") @@ -147,35 +147,35 @@ (parameterize ((dynamic? #t)) (execute-test - "class X { int m(dynamic x) { return x(1); } }" + "class Xf { int m(dynamic x) { return x(1); } }" 'full #f "Using a dynamic parameter as a method")) (parameterize ((dynamic? #t)) (execute-test - "class X { dynamic x; }" + "class Xg { dynamic x; }" 'full #f "Dynamic variable (unused) in class") (execute-test - "class X { dynamic x; int foo() { return x; } }" + "class Xh { dynamic x; int foo() { return x; } }" 'full #f "Dynamic variable used, but not executed in class") (execute-test - "class X { dynamic f() { return 3; } }" + "class Xi { dynamic f() { return 3; } }" 'full #f "Method returning dynamic with actual an int") (execute-test - "class X { int f(dynamic x) { return 3; }}" + "class Xj { int f(dynamic x) { return 3; }}" 'full #f "Method with dynamic parm, not used") (execute-test - "class X {float f(dynamic x, dynamic y) { return x + y; }}" + "class Xk {float f(dynamic x, dynamic y) { return x + y; }}" 'full #f "Method adding two dynamics, returning a float") (interact-test - "class X { float f( dynamic x, dynamic y) { return x + y; }}" - 'full (list "new X().f(1,1);") + "class Xl { float f( dynamic x, dynamic y) { return x + y; }}" + 'full (list "new Xl().f(1,1);") (list 2) "Method adding two dynamics (returning a float), called")) (execute-test - "class C { + "class Crv { void x() { return 1; } }" 'full #t "Trying to return value from void method") @@ -187,7 +187,7 @@ "Make sure returns are type-checked in interactions") (execute-test - "class A { + "class Abames { void n() { } void s() { } void src() { } @@ -196,15 +196,15 @@ 'full #f "Names that used to get clobbered") (interact-test - "class A { + "class Ainner { class B { B() { } - A m = A.this; + Ainner m = Ainner.this; } //B b = new B(); }" 'full - (list "A a = new A();" "A.B b = a.new B();" "a.new B().m") + (list "Ainner a = new Ainner();" "Ainner.B b = a.new B();" "a.new B().m") (list '(void) '(void) 'a~f) "Inner class creation") @@ -233,15 +233,15 @@ }" 'full #f "Instanceof test") - (execute-test "interface F { + (execute-test "interface Faa { int foo(int x); } - interface G extends F { + interface Gaa extends Faa { int foo(int x); } - class A implements G { - A() { } + class Aia implements Gaa { + Aia() { } int foo(int x) { return 3; } }" 'full #f "Extending an interface while overriding a method") @@ -253,19 +253,19 @@ 'full #f "Static access and order") (interact-test - "public class hasStatic { + "public class hasStatic1 { private int myId; - public hasStatic( int id ) { + public hasStatic1( int id ) { super(); this.myId = id; } - public static int returnId( hasStatic s ) { + public static int returnId( hasStatic1 s ) { return s.myId; } }" - 'full (list "hasStatic.returnId(new hasStatic(4))") (list 4) "Static use of private field") + 'full (list "hasStatic1.returnId(new hasStatic1(4))") (list 4) "Static use of private field") (interact-test 'full (list "int x = 4;" "x") @@ -293,17 +293,17 @@ 'full #t "Misplaced super call") (interact-test - "class A { + "class Az { static int x= 0; static { for(int i = 0; i< 10; i++) x += i; } }" - 'full (list "A.x") (list 45) "for loop in static section") + 'full (list "Az.x") (list 45) "for loop in static section") (execute-test - "class A { A() { super.toString(); } }" + "class Azz { Azz() { super.toString(); } }" 'full #f "Calling a super method") (report-test-results)) \ No newline at end of file diff --git a/collects/tests/profj/intermediate-tests.ss b/collects/tests/profj/intermediate-tests.ss index 1d9c4739c4..008c1396ad 100644 --- a/collects/tests/profj/intermediate-tests.ss +++ b/collects/tests/profj/intermediate-tests.ss @@ -259,9 +259,9 @@ 'intermediate #f "Abstract method implemented, class subclassed") (execute-test - "class X { + "class XIof { boolean equals( Object o ) { - return o instanceof X; + return o instanceof XIof; } }" 'intermediate #f "Correct instanceof usage") @@ -429,14 +429,14 @@ }" 'intermediate #t "Incompatible return type from inherited interface") (execute-test - "class X { + "class X2 { int c (Object o) { return (int) o; } }" 'intermediate #t "Cast of object to primitive") (execute-test - "class X { + "class X3 { int c () { return (int) false; } @@ -445,7 +445,7 @@ (execute-test "interface A { int x();} interface B { boolean x(); } - class X { + class X4 { Object o(A a) { return (B) a; }