All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org, Eduardo Habkost <ehabkost@redhat.com>,
	Michael Roth <mdroth@linux.vnet.ibm.com>,
	Cleber Rosa <crosa@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v4 07/51] qapi: leave the ifcond attribute undefined until check()
Date: Tue, 06 Feb 2018 12:20:18 +0100	[thread overview]
Message-ID: <87a7wmcqkt.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20180111213250.16511-8-marcandre.lureau@redhat.com> ("Marc-André Lureau"'s message of "Thu, 11 Jan 2018 22:32:06 +0100")

Second thoughts...

Marc-André Lureau <marcandre.lureau@redhat.com> writes:

> We commonly initialize attributes to None in .init(), then set their
> real value in .check().  Accessing the attribute before .check()
> yields None.  If we're lucky, the code that accesses the attribute
> prematurely chokes on None.
>
> It won't for .ifcond, because None is a legitimate value.
>
> Leave the ifcond attribute undefined until check().
>
> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  scripts/qapi.py | 21 +++++++++++++++++----
>  1 file changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/scripts/qapi.py b/scripts/qapi.py
> index 8f54dead8d..4d2c214f19 100644
> --- a/scripts/qapi.py
> +++ b/scripts/qapi.py
> @@ -1011,13 +1011,19 @@ class QAPISchemaEntity(object):
>          # such place).
>          self.info = info
>          self.doc = doc
> -        self.ifcond = listify_cond(ifcond)
> +        self._ifcond = ifcond  # self.ifcond is set after check()
>  
>      def c_name(self):
>          return c_name(self.name)
>  
>      def check(self, schema):
> -        pass
> +        if isinstance(self._ifcond, QAPISchemaType):
> +            # inherit the condition from a type
> +            typ = self._ifcond
> +            typ.check(schema)
> +            self.ifcond = typ.ifcond
> +        else:
> +            self.ifcond = listify_cond(self._ifcond)

Whenever we add a .check(), we need to prove QAPISchema.check()'s
recursion still terminates, and terminates the right way.

Argument before this patch: we recurse only into types contained in
types, e.g. an object type's base type, and we detect and report cycles
as "Object %s contains itself", in QAPISchemaObjectType.check().

The .check() added here recurses into a type.  If this creates a cycle,
it'll be caught and reported as "contains itself".  We still need to
show that the error message remains accurate.

We .check() here to inherit .ifcond from a type.  As far as I can tell,
we use this inheritance feature only to inherit an array's condition
from its element type.  That's safe, because an array does contain its
elements.

This is hardly a rigorous proof.  Just enough to make me believe your
code works.

However, I suspect adding the inheritance feature at the entity level
complicates the correctness argument without real need.  Can we restrict
it to array elements?  Have QAPISchemaArrayType.check() resolve
type-valued ._ifcond, and all the others choke on it?

>  
>      def is_implicit(self):
>          return not self.info
> @@ -1138,6 +1144,7 @@ class QAPISchemaEnumType(QAPISchemaType):
>          self.prefix = prefix
>  
>      def check(self, schema):
> +        QAPISchemaType.check(self, schema)
>          seen = {}
>          for v in self.values:
>              v.check_clash(self.info, seen)
> @@ -1170,8 +1177,10 @@ class QAPISchemaArrayType(QAPISchemaType):
>          self.element_type = None
>  
>      def check(self, schema):
> +        QAPISchemaType.check(self, schema)
>          self.element_type = schema.lookup_type(self._element_type_name)
>          assert self.element_type
> +        self.element_type.check(schema)

This .check is locally obvious: an array contains its elements.

>          self.ifcond = self.element_type.ifcond
>  
>      def is_implicit(self):
> @@ -1214,6 +1223,7 @@ class QAPISchemaObjectType(QAPISchemaType):
>          self.members = None
>  
>      def check(self, schema):
> +        QAPISchemaType.check(self, schema)
>          if self.members is False:               # check for cycles
>              raise QAPISemError(self.info,
>                                 "Object %s contains itself" % self.name)
> @@ -1396,6 +1406,7 @@ class QAPISchemaAlternateType(QAPISchemaType):
>          self.variants = variants
>  
>      def check(self, schema):
> +        QAPISchemaType.check(self, schema)
>          self.variants.tag_member.check(schema)
>          # Not calling self.variants.check_clash(), because there's nothing
>          # to clash with
> @@ -1438,6 +1449,7 @@ class QAPISchemaCommand(QAPISchemaEntity):
>          self.boxed = boxed
>  
>      def check(self, schema):
> +        QAPISchemaEntity.check(self, schema)
>          if self._arg_type_name:
>              self.arg_type = schema.lookup_type(self._arg_type_name)
>              assert (isinstance(self.arg_type, QAPISchemaObjectType) or
> @@ -1471,6 +1483,7 @@ class QAPISchemaEvent(QAPISchemaEntity):
>          self.boxed = boxed
>  
>      def check(self, schema):
> +        QAPISchemaEntity.check(self, schema)
>          if self._arg_type_name:
>              self.arg_type = schema.lookup_type(self._arg_type_name)
>              assert (isinstance(self.arg_type, QAPISchemaObjectType) or
> @@ -1589,7 +1602,7 @@ class QAPISchema(object):
>              # But it's not tight: the disjunction need not imply it.  We
>              # may end up compiling useless wrapper types.
>              # TODO kill simple unions or implement the disjunction
> -            assert ifcond == typ.ifcond
> +            assert ifcond == typ._ifcond

pylint complains

    W:1605,29: Access to a protected member _ifcond of a client class (protected-access)

Layering violation.  Tolerable, I think.

>          else:
>              self._def_entity(QAPISchemaObjectType(name, info, doc, ifcond,
>                                                    None, members, None))
> @@ -1635,7 +1648,7 @@ class QAPISchema(object):
>              assert len(typ) == 1
>              typ = self._make_array_type(typ[0], info)
>          typ = self._make_implicit_object_type(
> -            typ, info, None, self.lookup_type(typ).ifcond,
> +            typ, info, None, self.lookup_type(typ),
>              'wrapper', [self._make_member('data', typ, info)])
>          return QAPISchemaObjectTypeVariant(case, typ)

  parent reply	other threads:[~2018-02-06 11:20 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-11 21:31 [Qemu-devel] [PATCH v4 00/51] Hi, Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 01/51] qlit: use QType instead of int Marc-André Lureau
2018-01-11 22:52   ` Eric Blake
2018-02-05  6:05   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 02/51] qlit: add qobject_from_qlit() Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 03/51] qapi: generate a literal qobject for introspection Marc-André Lureau
2018-02-06 10:04   ` Markus Armbruster
2018-02-06 11:01     ` Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 04/51] qapi2texi: minor python code simplification Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 05/51] qapi: add 'if' to top-level expressions Marc-André Lureau
2018-02-05  6:12   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 06/51] qapi: pass 'if' condition into QAPISchemaEntity objects Marc-André Lureau
2018-02-05  6:16   ` Markus Armbruster
2018-02-06 10:12   ` Markus Armbruster
2018-02-06 11:06     ` Marc-André Lureau
2018-02-06 12:14       ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 07/51] qapi: leave the ifcond attribute undefined until check() Marc-André Lureau
2018-02-05  6:22   ` Markus Armbruster
2018-02-06 11:20   ` Markus Armbruster [this message]
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 08/51] qapi: add 'ifcond' to visitor methods Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 09/51] qapi: mcgen() shouldn't indent # lines Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 10/51] qapi: add #if/#endif helpers Marc-André Lureau
2018-02-05  7:03   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 11/51] qapi-introspect: modify to_qlit() to append ', ' on level > 0 Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 12/51] qapi-introspect: add preprocessor conditions to generated QLit Marc-André Lureau
2018-01-12 10:27   ` Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 13/51] qapi-commands: add #if conditions to commands Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 14/51] qapi-event: add #if conditions to events Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 15/51] qapi-types: refactor variants handling Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 16/51] qapi-types: add #if conditions to types & visitors Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 17/51] qapi: do not define enumeration value explicitely Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 18/51] qapi: rename QAPISchemaEnumType.values to .members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 19/51] qapi: change enum visitor to take QAPISchemaMember Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 20/51] tests: modify visit_enum_type() in test-qapi to print members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 21/51] qapi: factor out check_known_keys() Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 22/51] qapi: add a dictionnary form with 'name' key for enum members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 23/51] qapi: add 'if' to " Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 24/51] qapi-event: add 'if' condition to implicit event enum Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 25/51] qapi: rename allow_dict to allow_implicit Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 26/51] qapi: add a dictionary form with 'type' key for members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 27/51] qapi: add 'if' to implicit struct members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 28/51] qapi: add an error in case a discriminator is conditionnal Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 29/51] qapi: add 'if' on union members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 30/51] qapi: add 'if' to alternate members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 31/51] qapi: add #if conditions to generated code Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 32/51] docs: document schema configuration Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 33/51] qapi2texi: add 'If:' section to generated documentation Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 34/51] qapi2texi: add 'If:' condition to enum values Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 35/51] qapi2texi: add 'If:' condition to struct members Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 36/51] qapi2texi: add condition to variants Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 37/51] qapi: add conditions to VNC type/commands/events on the schema Marc-André Lureau
2018-01-12 13:04   ` Gerd Hoffmann
2018-01-12 13:22     ` Marc-André Lureau
2018-01-12 13:55       ` Gerd Hoffmann
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 38/51] qapi: add conditions to SPICE " Marc-André Lureau
2018-01-12 13:09   ` Gerd Hoffmann
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 39/51] qapi: add conditions to REPLICATION type/commands " Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 40/51] qapi-commands: don't initialize command list in qmp_init_marshall() Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 41/51] qapi: add -i/--include filename.h Marc-André Lureau
2018-02-05 16:58   ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 42/51] qapi: add a 'unit' pragma Marc-André Lureau
2018-01-12 11:54   ` Marc-André Lureau
2018-02-05 18:13     ` Markus Armbruster
2018-02-06 11:01       ` Marc-André Lureau
2018-02-06 12:31         ` Markus Armbruster
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 43/51] build-sys: move qmp-introspect per target Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 44/51] build-sys: add a target schema Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 45/51] qapi: make rtc-reset-reinjection depend on TARGET_I386 Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 46/51] qapi: make s390 commands depend on TARGET_S390X Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 47/51] target.json: add a note about query-cpu* not being s390x-specific Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 48/51] qapi: make query-gic-capabilities depend on TARGET_ARM Marc-André Lureau
2018-01-11 21:32   ` Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 49/51] qapi: make query-cpu-model-expansion depend on s390 or x86 Marc-André Lureau
2018-01-11 21:32 ` [Qemu-arm] [PATCH v4 50/51] qapi: make query-cpu-definitions depend on specific targets Marc-André Lureau
2018-01-11 21:32   ` [Qemu-devel] " Marc-André Lureau
2018-01-11 21:32 ` [Qemu-devel] [PATCH v4 51/51] qapi: remove qmp_unregister_command() Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a7wmcqkt.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.