[CXX-1152] Context enforcement for BSON stream builder doesn't play nicely with statements that leave open sub-documents or sub-arrays Created: 01/Dec/16 Updated: 20/Apr/17 Resolved: 20/Apr/17 |
|
| Status: | Closed |
| Project: | C++ Driver |
| Component/s: | BSON, Documentation |
| Affects Version/s: | None |
| Fix Version/s: | 3.2.0-rc0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | J Rassi | Assignee: | Samuel Rossi (Inactive) |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Description |
|
BSON stream builders feature the ability to stream keys and values across multiple statements, including values that leave open new sub-documents and sub-arrays. As a result, the type system cannot have knowledge of what the current level of depth is for a BSON stream builder, or what the type of its current context is. This leads to unintuitive behaviors. Consider the following code snippet (modified from the test case "[] with large nesting levels") that uses a document stream builder to build the document {"a": {"b": {"c": 1}}}:
This above code snippet correctly builds this document. However, this code compiles only because all of the above statements happen to be valid in key context (since the type of "doc" is a derived class of bsoncxx::builder::stream::key_context). Things start to get wonky when attempting to use stream builders in a statement where multiple levels of depth are backtracked:
Or, when a statement leaves open a sub-document or sub-array which provides a different context than the top-level BSON datum type provides:
The fact that "example 1" compiles and runs but "example 2" and "example 3" fail to compile reveals a fundamental design flaw in the stream builder. The stream builder should be changed such that either all three examples compile, or all three examples fail to compile. |
| Comments |
| Comment by Githook User [ 20/Apr/17 ] | ||||||||
|
Author: {u'username': u'saghm', u'name': u'Saghm Rossi', u'email': u'saghmrossi@gmail.com'}Message: | ||||||||
| Comment by David Golden [ 14/Apr/17 ] | ||||||||
|
Let's fix this via documentation that explains it and warns users not to do it, rather than trying to fix it in the code. We're generally discouraging the stream builder approach for general use anyway. | ||||||||
| Comment by Andrew Morrow (Inactive) [ 03/Dec/16 ] | ||||||||
|
I would consider both examples where the intermediate result type was not captured to be ill formed - the fact that they work is accidental. How about using _attribute_((warn_unused_result)) (via an appropriate compiler macro) on the functions that return context that is intended to be captured? | ||||||||
| Comment by J Rassi [ 01/Dec/16 ] | ||||||||
|
My current ideas of possible solutions for this issue are as follows (all three examples compile with solution #1, whereas all three examples fail to compile with solutions #2-#5):
|