Merge: Less old style init special cases
authorJean Privat <jean@pryen.org>
Mon, 29 Sep 2014 17:07:35 +0000 (13:07 -0400)
committerJean Privat <jean@pryen.org>
Mon, 29 Sep 2014 17:07:35 +0000 (13:07 -0400)
The general transition to new constructor is nearer each day.

May fix an issue informally signaled by @R4PaSs.

Pull-Request: #783
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

24 files changed:
contrib/friendz/src/grid.nit
contrib/pep8analysis/src/cfg/cfg_base.nit
examples/circular_list.nit
lib/android/audio.nit
lib/for_abuse.nit
lib/geometry/points_and_lines.nit
lib/mnit/tileset.nit
lib/neo4j/neo4j.nit
lib/nitcc_runtime.nit
lib/pipeline.nit
lib/socket/socket.nit
lib/standard/collection/array.nit
lib/standard/collection/list.nit
lib/standard/exec.nit
lib/standard/ropes.nit
lib/standard/stream.nit
lib/standard/string.nit
src/modelize/modelize_property.nit
tests/example_objet.nit
tests/example_point.nit
tests/sav/base_init_combine_alt1.res
tests/sav/error_init_auto_alt4.res
tests/test_create.nit
tests/test_create_more.nit

index 3913154..a097ff5 100644 (file)
@@ -13,13 +13,13 @@ module grid
 # Grid of monsters.
 class Grid
        # width of the current grid
-       var width: Int
+       var width: Int is noinit
 
        # maximum width of the grid
        var max_width: Int
 
        # height of the current grid
-       var height: Int
+       var height: Int is noinit
 
        # maximum height of the grid
        var max_height: Int
@@ -30,13 +30,7 @@ class Grid
        # the data grid
        private var grid = new Array[Array[Tile]]
 
-       init(mw,mh,nm: Int)
-       do
-               self.max_width = mw
-               self.max_height = mh
-               self.nb_monsters = mh
-               clear
-       end
+       init do clear
 
        # Reinitialize the grid with new empty tiles and monsters info
        fun clear
index c4de901..d42a4b3 100644 (file)
@@ -376,7 +376,7 @@ class CFG
 end
 
 class BasicBlock
-       var name : String
+       var name: String is noinit
        var lines = new Array[ANonEmptyLine]
        var successors = new Array[BasicBlock]
        var predecessors = new Array[BasicBlock]
index c3ba1ed..dd4e54d 100644 (file)
@@ -120,11 +120,11 @@ end
 private class CircularListIterator[E]
        super IndexedIterator[E]
 
-       redef var index: Int
+       redef var index: Int = 0
 
        # The current node pointed.
        # Is null if the list is empty.
-       var node: nullable CLNode[E]
+       var node: nullable CLNode[E] is noinit
 
        # The list iterated.
        var list: CircularList[E]
@@ -144,11 +144,9 @@ private class CircularListIterator[E]
 
        redef fun item do return self.node.item
 
-       init(list: CircularList[E])
+       init
        do
                self.node = list.node
-               self.list = list
-               self.index = 0
        end
 end
 
index 8d3264f..e640621 100644 (file)
@@ -232,10 +232,10 @@ end
 # The Android MediaPlayer has a complex state diagram that you'll need to
 # respect if you want your MediaPlayer to work fine, see the android doc
 class MediaPlayer
-       private var nmedia_player: NativeMediaPlayer
+       private var nmedia_player: NativeMediaPlayer is noinit
 
        # The sound associated with this mediaplayer
-       var sound: nullable Sound
+       var sound: nullable Sound = null
 
        # Create a new MediaPlayer, but no sound is attached, you'll need
        # to use `load_sound` before using it
index 2ee4d28..eeb8ed7 100644 (file)
@@ -46,9 +46,10 @@ end
 # Abuser iterator to read a file, see `file_open`
 private class ReadFileForAbuserIterator
        super Iterator[IFStream]
-       redef var item: IFStream
+       var path: String
+       redef var item: IFStream is noinit
        redef var is_ok = true
-       init(path: String)
+       init
        do
                # start of service is to open the file, and return in
                item = new IFStream.open(path)
@@ -94,21 +95,18 @@ end
 private class SortAbuserIterator[E]
        super Iterator[CompareQuery[E]]
        # The index of the big loop
-       var i: Int
+       var i: Int = 0
        # The index of the small loop
-       var j: Int
+       var j: Int = 0
        # The array to sort
        var array: Array[E]
        # The query used to communicate with the user.
        # For ecological concerns, a unique CompareQuery is instatiated.
-       var query: nullable CompareQuery[E]
+       var query: nullable CompareQuery[E] = null
        redef fun item do return query.as(not null)
-       init(array: Array[E])
+       init
        do
-               self.array = array
                # Initialize the algorithm, see `next` for the rest
-               i = 0
-               j = 0
                if not is_ok then return
                query = new CompareQuery[E](array[i], array[j])
        end
index 492d31f..14a67fb 100644 (file)
@@ -33,12 +33,6 @@ class Point[N: Numeric]
 
        redef var x: N
        redef var y: N
-
-       init(x, y: N)
-       do
-               self.x = x
-               self.y = y
-       end
 end
 
 # An abstract 3d point, strongly linked to its implementation `Point3d`
@@ -57,12 +51,6 @@ class Point3d[N: Numeric]
        super Point[N]
 
        redef var z: N
-
-       init(x, y, z: N)
-       do
-               super
-               self.z = z
-       end
 end
 
 # An abstract 2d line segment
@@ -82,12 +70,11 @@ class Line[N: Numeric]
        redef var point_left: P
        redef var point_right: P
 
-       init(a, b: P)
+       init
        do
-               if a.x < b.x then
-                       point_left = a
-                       point_right = b
-               else
+               var a = point_left
+               var b = point_right
+               if a.x > b.x then
                        point_left = b
                        point_right = a
                end
index 8495c51..568fad5 100644 (file)
@@ -28,12 +28,8 @@ class TileSet
        # The height of a tile
        var height: Int
 
-       init(image: Image, width: Int, height: Int)
+       init
        do
-               self.image = image
-               self.width = width
-               self.height = height
-
                self.nb_cols = image.width / width
                self.nb_rows = image.height / height
 
@@ -45,10 +41,10 @@ class TileSet
        end
 
        # The number of columns of tiles in the image
-       var nb_cols: Int
+       var nb_cols: Int is noinit
 
        # The number of rows of tiles in the image
-       var nb_rows: Int
+       var nb_rows: Int is noinit
 
        # Cache for images of tiles
        var subimages = new Array[Image]
@@ -72,12 +68,6 @@ class TileSetFont
        # Use space (' ') for holes in the tileset
        var chars: String
 
-       init(image: Image, width: Int, height: Int, chars: String)
-       do
-               super
-               self.chars = chars
-       end
-
        # Additional space to insert horizontally between characters
        # A negave value will display tile overlaped
        var hspace: Int = 0 is writable
index 41fc26b..385b966 100644 (file)
@@ -384,14 +384,12 @@ end
 # For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
 class CypherQuery
        # Query string to perform
-       private var query: String
+       private var query: String = ""
 
        # `params` to embed in the query like in prepared statements
        var params = new JsonObject
 
-       init do
-               self.query = ""
-       end
+       init do end
 
        # init the query from a query string
        init from_string(query: String) do
@@ -488,10 +486,10 @@ end
 #     assert node["name"] == "Andres"  # loaded lazily from base
 abstract class NeoEntity
        # Neo4j client connector
-       private var neo: Neo4jClient
+       private var neo: Neo4jClient is noinit
 
        # Entity unique URL in Neo4j REST API
-       var url: nullable String
+       var url: nullable String = null
 
        # Temp id used in batch mode to update the entity
        private var batch_id: nullable Int = null
index 74dcd43..9c5ec01 100644 (file)
@@ -276,7 +276,6 @@ class TreePrinterVisitor
        super Visitor
        var writer: OStream
        private var indent = 0
-       init(writer: OStream) do self.writer = writer
        redef fun visit(n)
        do
                for i in [0..indent[ do writer.write("  ")
index 0c422ed..da1b4c4 100644 (file)
@@ -294,13 +294,7 @@ private class PipeSkip[E]
        var source: Iterator[E]
        var skip_item: E
 
-       init(source: Iterator[E], skip_item: E)
-       do
-               self.source = source
-               self.skip_item = skip_item
-
-               do_skip
-       end
+       init do do_skip
 
        fun do_skip
        do
@@ -345,10 +339,8 @@ private class PipeSkipTail[E]
 
        var lasts = new List[E]
 
-       init(source: Iterator[E], length: Int)
+       init
        do
-               self.source = source
-               self.length = length
                var lasts = self.lasts
                while source.is_ok and lasts.length < length do
                        lasts.push(source.item)
@@ -375,13 +367,7 @@ private class PipeSelect[E]
 
        var predicate: Function[E, Bool]
 
-       init(source: Iterator[E], predicate: Function[E, Bool])
-       do
-               self.source = source
-               self.predicate = predicate
-
-               do_skip
-       end
+       init do do_skip
 
        fun do_skip
        do
index 7dee2ef..aad6449 100644 (file)
@@ -28,20 +28,20 @@ class Socket
 
        # IPv4 address the socket is connected to
        # Formatted as xxx.xxx.xxx.xxx
-       var address: String
+       var address: String is noinit
 
        # Hostname of the socket connected to self
        # In C : The real canonical host name (e.g. example.org)
-       var host: nullable String
+       var host: nullable String = null
 
        # Port open for the socket
-       var port: Int
+       var port: Int is noinit
 
        # Underlying C socket
-       private var socket: FFSocket
+       private var socket: FFSocket is noinit
 
        # Underlying C socket
-       private var addrin: FFSocketAddrIn
+       private var addrin: FFSocketAddrIn is noinit
 
        redef var end_reached = false
 
index 61e8700..f106b25 100644 (file)
@@ -416,7 +416,7 @@ class ArraySet[E: Object]
        super Set[E]
 
        # The stored elements.
-       private var array: Array[E]
+       private var array: Array[E] is noinit
 
        redef fun has(e) do return _array.has(e)
 
index 08a8459..4487ec3 100644 (file)
@@ -210,10 +210,10 @@ class List[E]
        init from(coll: Collection[E]) do append(coll)
 
        # The first node of the list
-       private var head: nullable ListNode[E]
+       private var head: nullable ListNode[E] = null
 
        # The last node of the list
-       private var tail: nullable ListNode[E]
+       private var tail: nullable ListNode[E] = null
 
        # Get the `i`th node. get `null` otherwise.
        private fun get_node(i: Int): nullable ListNode[E]
index 347e123..150ed48 100644 (file)
@@ -92,7 +92,7 @@ end
 class IProcess
        super Process
        super IStream
-       var stream_in: FDIStream
+       var stream_in: FDIStream is noinit
 
        redef fun close do stream_in.close
 
@@ -102,8 +102,6 @@ class IProcess
 
        redef fun pipeflags do return 2
 
-       redef init(command: String, arguments: String...) do super
-
        redef fun execute
        do
                super
@@ -115,7 +113,7 @@ end
 class OProcess
        super Process
        super OStream
-       var stream_out: OStream
+       var stream_out: OStream is noinit
 
        redef fun close do stream_out.close
 
@@ -125,8 +123,6 @@ class OProcess
 
        redef fun pipeflags do return 1
 
-       redef init(command: String, arguments: String...) do super
-
        redef fun execute
        do
                super
@@ -148,8 +144,6 @@ class IOProcess
 
        redef fun pipeflags do return 3
 
-       redef init(command: String, arguments: String...) do super
-
        redef fun execute
        do
                super
index 843096f..ad97500 100644 (file)
@@ -114,7 +114,7 @@ abstract class Rope
        super Text
 
        # Root node, entry point of a Rope.
-       private var root: RopeNode
+       private var root: RopeNode is noinit
 
        # Cached version of self as a flat String
        private var str_representation: nullable NativeString = null
index 5932daa..6b70e21 100644 (file)
@@ -288,8 +288,6 @@ abstract class FDStream
        private fun native_read(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
        private fun native_write(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
        private fun native_write_char(i: Int, c: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
-
-       init(fd: Int) do self.fd = fd
 end
 
 class FDIStream
@@ -303,36 +301,24 @@ class FDIStream
                if nb == -1 then eof = true
                return nb
        end
-
-       init(fd: Int) do end 
 end
 
 class FDOStream
        super FDStream
        super OStream
-       redef var is_writable: Bool
+       redef var is_writable = true
 
        redef fun write(s)
        do
                var nb = native_write(fd, s.to_cstring, s.length)
                if nb < s.length then is_writable = false
        end
-
-       init(fd: Int)
-       do
-               is_writable = true
-       end
 end
 
 class FDIOStream
        super FDIStream
        super FDOStream
        super IOStream
-       init(fd: Int)
-       do
-               self.fd = fd
-               is_writable = true
-       end
 end
 
 redef interface Object
index a81bdd6..3eede86 100644 (file)
@@ -663,7 +663,7 @@ abstract class FlatText
        #
        # Warning : Might be void in some subclasses, be sure to check
        # if set before using it.
-       private var items: NativeString
+       private var items: NativeString is noinit
 
        # Real items, used as cache for to_cstring is called
        private var real_items: nullable NativeString = null
@@ -887,10 +887,10 @@ class FlatString
        super String
 
        # Index in _items of the start of the string
-       private var index_from: Int
+       private var index_from: Int is noinit
 
        # Indes in _items of the last item of the string
-       private var index_to: Int
+       private var index_to: Int is noinit
 
        redef var chars: SequenceRead[Char] = new FlatStringCharView(self)
 
index 99ff6b4..5542e91 100644 (file)
@@ -82,8 +82,6 @@ redef class ModelBuilder
                # Are we a refinement
                if not mclassdef.is_intro then return
 
-               var mmodule = nclassdef.mclassdef.mmodule
-
                # Look for the init in Object, or create it
                if mclassdef.mclass.name == "Object" and the_root_init_mmethod == null then
                        # Create the implicit root-init method
@@ -112,41 +110,14 @@ redef class ModelBuilder
                        if mpropdef.mproperty.is_root_init then
                                assert defined_init == null
                                defined_init = mpropdef
-                       else
-                               # An explicit old-style init, so return
+                       else if mpropdef.mproperty.name == "init" then
+                               # An explicit old-style init named "init", so return
                                return
                        end
                end
 
                if not nclassdef isa AStdClassdef then return
 
-               # Do we inherit a old-style constructor?
-               var combine = new Array[MMethod] # old-style constructors without arguments
-               var inhc: nullable MClass = null # single super-class with a constructor with arguments
-               if defined_init == null then for st in mclassdef.supertypes do
-                       var c = st.mclass
-                       if not c.kind.need_init then continue
-                       st = st.anchor_to(mmodule, mclassdef.bound_mtype)
-                       var candidate = self.try_get_mproperty_by_name2(nclassdef, mmodule, st, "init").as(nullable MMethod)
-                       if candidate != null then
-                               if candidate.is_root_init then continue
-                               if candidate.intro.msignature != null then
-                                       if candidate.intro.msignature.arity == 0 then
-                                               combine.add(candidate)
-                                               continue
-                                       end
-                               end
-                       end
-                       var inhc2 = c.inherit_init_from
-                       if inhc2 == null then inhc2 = c
-                       if inhc2 == inhc then continue
-                       if inhc != null then
-                               self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {inhc} and {c}")
-                       else
-                               inhc = inhc2
-                       end
-               end
-
                # Collect undefined attributes
                var mparameters = new Array[MParameter]
                var initializers = new Array[MProperty]
@@ -181,44 +152,9 @@ redef class ModelBuilder
                end
                if anode == null then anode = nclassdef
 
-               if combine.is_empty and inhc != null then
-                       if not mparameters.is_empty then
-                               self.error(anode,"Error: {mclassdef} cannot inherit constructors from {inhc} because there is attributes without initial values: {mparameters.join(", ")}")
-                               return
-                       end
-
-                       # TODO: actively inherit the consturctor
-                       self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
-                       #mclassdef.mclass.inherit_init_from = inhc
-                       #return
-               end
-
-               if not combine.is_empty and inhc != null then
-                       self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {combine.join(", ")} and {inhc}")
-                       return
-               end
-               if not combine.is_empty then
-                       if mparameters.is_empty and combine.length == 1 then
-                               # No need to create a local init, the inherited one is enough
-                               inhc = combine.first.intro_mclassdef.mclass
-                               mclassdef.mclass.inherit_init_from = inhc
-                               self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
-                               return
-                       end
-                       nclassdef.super_inits = combine
-                       var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
-                       var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
-                       var msignature = new MSignature(mparameters, null)
-                       mpropdef.msignature = msignature
-                       mprop.is_init = true
-                       nclassdef.mfree_init = mpropdef
-                       self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
-                       return
-               end
-
                if the_root_init_mmethod == null then return
 
-               # Look for nost-specific new-stype init definitions
+               # Look for most-specific new-stype init definitions
                var spropdefs = the_root_init_mmethod.lookup_super_definitions(mclassdef.mmodule, mclassdef.bound_mtype)
                if spropdefs.is_empty then
                        toolcontext.fatal_error(nclassdef.location, "Fatal error: {mclassdef} does not specialize {the_root_init_mmethod.intro_mclassdef}. Possible duplication of the root class `Object`?")
@@ -328,12 +264,6 @@ redef class ModelBuilder
        end
 end
 
-redef class MClass
-       # The class whose self inherit all the constructors.
-       # FIXME: this is needed to implement the crazy constructor mixin thing of the of old compiler. We need to think what to do with since this cannot stay in the modelbuilder
-       var inherit_init_from: nullable MClass = null
-end
-
 redef class MPropDef
        # Does the MPropDef contains a call to super or a call of a super-constructor?
        # Subsequent phases of the frontend (esp. typing) set it if required
index 891a3d9..7473896 100644 (file)
@@ -62,8 +62,8 @@ private
 # receveur ("self")
 
 # Les attributs sont déclarés par le mot clé "fun" et commencent par un "@"
-       var nom_: String                # Un entrepôt a un nom (de type chaîne).
-       var rayons: Array[Rayon]        # Il est composé d'un ensemble de rayon.
+       var nom_: String is noinit              # Un entrepôt a un nom (de type chaîne).
+       var rayons: Array[Rayon] is noinit      # Il est composé d'un ensemble de rayon.
        # "Array" est une classe paramétrée, les crochets en sont la marque.
        # La classe "Rayon" est définie plus loin
 
index 2ad57c5..a7b89ea 100644 (file)
@@ -16,8 +16,8 @@
 
 class Point
 private
-       var x: Int      # Abscisse
-       var y: Int      # Ordonnée
+       var x: Int = 0  # Abscisse
+       var y: Int = 0  # Ordonnée
 
        # Change la position d'un point
        fun moveto(x: Int, y: Int)
@@ -38,11 +38,6 @@ private
        end
 
 
-       init
-       do
-               moveto(0, 0)
-       end
-
        init at(x: Int, y: Int)
        do
                moveto(x, y)
index 5759dba..510c2f5 100644 (file)
@@ -1 +1,9 @@
-alt/base_init_combine_alt1.nit:52,6: Error: base_init_combine_alt1#F cannot inherit constructors from E because there is attributes without initial values: z: Int
+1
+2
+2
+3
+2
+2
+5
+2
+6
index 0bb1a5e..65c6fb1 100644 (file)
@@ -1,3 +1,3 @@
-alt/error_init_auto_alt4.nit:35,5--12: Error: Incorrect number of parameters. Got 1, expected 0. Signature is 
-alt/error_init_auto_alt4.nit:36,5--15: Error: Incorrect number of parameters. Got 2, expected 0. Signature is 
-alt/error_init_auto_alt4.nit:37,5--18: Error: Incorrect number of parameters. Got 3, expected 0. Signature is 
+alt/error_init_auto_alt4.nit:34,5--9: Error: Incorrect number of parameters. Got 0, expected 1. Signature is (x: Int)
+alt/error_init_auto_alt4.nit:36,5--15: Error: Incorrect number of parameters. Got 2, expected 1. Signature is (x: Int)
+alt/error_init_auto_alt4.nit:37,5--18: Error: Incorrect number of parameters. Got 3, expected 1. Signature is (x: Int)
index c41c81a..0eee185 100644 (file)
@@ -24,7 +24,7 @@ redef class Object
 end
 
 class Toto
-       var a: Int
+       var a: Int is noinit
        redef fun output
        do
                printn(_a)
index 844ffee..d870cea 100644 (file)
@@ -15,8 +15,8 @@
 # limitations under the License.
 
 class A
-       var attribute: nullable A
-       var num: Char
+       var attribute: nullable A is noinit
+       var num: Char is noinit
 
        fun foo=(a: nullable A)
        do