[lldb-dev] Renaming lldb_private::Error
Lang Hames via lldb-dev
lldb-dev at lists.llvm.org
Wed May 10 20:33:22 PDT 2017
Leaving 'Status' aside for now (the rename makes perfect sense), I'm basing
my ErrorAnd / WithError naming suggestion on this comment:
Is there any chance of introducing something like make_status<T>() into
> llvm/Error.h, that constructs the llvm::Error in such a way that it still
> interoperates nicely with everything else?
which contains a fundamental tension: Error's purpose is to be
un-ignorable, which could be considered "not nice", and is definitely at
odds with the idea of the user implicitly ignoring it if they want to
(though it can be explicitly ignored by calling consumeError).
But if it does need to be handled (and as such is called an error), then
> I'm not sure if it makes sense to say there's also a value. So ErrorOr, or
> Expected seems to convey that meaning in the only way possible. If you
> don't get the thing you're expected to get, you need to handle the error.
This is an aside from the LLDB conversation, but worth noting: While Error
instances must be dealt with, that doesn't mean Error is only useful for
hard errors. Being good for diagnostics was part of its original design
brief. The ErrorAnd concept comes into play any time you have a data
structure that can be partially malformed but still useful. Consider
libObject, for example: It should be able to parse partially malformed
object files (ones that have been truncated, or contain bad symbol indexes,
or malformed symbols, etc.). However you want to make sure that the client
explicitly acknowledges any errors in the file before proceeding (that way
they can't claim later that you didn't warn them. ;). ErrorAnd<ObjectFile>
is a perfect fit for this. It would take an Error (which may be success, a
singleton Error, or a compound Error) and an ObjectFile, and force you to
consume the Errors before accessing the file:
In pseudo-c++:
// Parse my object file.
auto ErrorAnd<ObjectFile> ObjAndErr = parseObjectFile(...);
// I claim that I'm willing to handle truncated objects, or objects
// containing bad symbol indexes. If the object file contains errors
// other than this I will bail out.
if (auto RemainingErrors = handleErrors(ObjAndErr.takeError(),
[](const BadSymbolIndex &BSI) { ...
},
[](const TruncatedObject &TO) { ...
}))
return RemainingErrors;
// Ok, *now* I can access my object:
auto Obj = ObjAndError.takeValue();
Again, I'm not recommending this for any specific LLDB interfaces (I don't
know them well enough yet), but I believe it has its place as an idiom.
Right. We know that at some point (at the least where they escape to the
> SB API's) we'll have to have a container for the content of the error which
> carries none of the programmatic imperatives we want to impose at lower
> layers.
The programmatic imperative is the key difference here.
Actually, Zachary - it's just occurred to me that that's what you may have
been asking for: A type that's structured like Error, but without the hard
requirement that it be checked? If so you're right - that might be an
interesting thing to add to llvm/Error.h.
Instead, you could do this nicely by making a Status object that can
> accumulate (and by doing so check) the errors from its primitive
> operations. You'd also have to be able to mark the Status as Succeeding.
> Then the composite operation could turn the Status back into an Error, or
> pass it out as a Status, depending on its contract. So I think it is
> worthwhile keep two separate entities.
For a function that returns an Error, and whose callees return Error, I
think you could just stay in Error mode the whole way through. Error
supports compound values, and there are idioms for accumulating them with
arbitrary checking applied at the accumulation point. Having an easy
conversion utility between Error and Status is definitely a must though.
Cheers,
Lang.
On Wed, May 10, 2017 at 7:36 PM, Jim Ingham <jingham at apple.com> wrote:
>
> > On May 10, 2017, at 6:28 PM, Zachary Turner <zturner at google.com> wrote:
> >
> > Yes, this is just the rename.
> >
> > Regarding the naming, if you call it ErrorAnd, or WithError, or anything
> that includes the word error, you are implying that something actually went
> wrong. I don't think that's the intended use case, or at least not what I
> have in mind (and from previous conversations on the list, I don't think
> what Jim had in mind either).
> >
> > If we're going to say that something does not need to be handled, I
> don't know if we should be calling it an error at all. By definition, we
> should assert that errors must be handled, so the converse is that if it
> doesn't need to be handled, it's not an error.
> >
> > But if it does need to be handled (and as such is called an error), then
> I'm not sure if it makes sense to say there's also a value. So ErrorOr, or
> Expected seems to convey that meaning in the only way possible. If you
> don't get the thing you're expected to get, you need to handle the error.
> >
> > But it seemed like what we were talking was more of a way to provide
> diagnostic information about a long process that you could return alongside
> a result. And if you don't get one, you don't necessarily care. So it's
> like one step down in the expectation chain from Expected. Possible<T>
> maybe?
> >
> > I would expect an interface similar to Optional<T>, but with a way to
> get error *like* diagnostic information or messages that the user could
> ignore if they wanted to.
>
> Right. We know that at some point (at the least where they escape to the
> SB API's) we'll have to have a container for the content of the error which
> carries none of the programmatic imperatives we want to impose at lower
> layers. So the plan as I understood it was to first declare that all
> Errors are actually what we give out to that layer. So we will rename them
> to Status, since that makes more sense at the outer level. 'Course until we
> get to SB API 2.0 we can't ACTUALLY rename the type we give out to the SB
> layer, but since we had to have two names and that's what we want to
> convey, we might as well change it as far out as we can... Then we'll go
> back and start from the leaves and chase up as far as is reasonable
> converting to force checked errors. My guess is the point at which we'll
> find propagating checked errors starts to get annoying will be before the
> SB API layer anyway, though we'll see about that.
>
> But I think the "Status" object is more generally useful.
>
> For instance, there are places where we try a bunch of things to perform a
> task for the user: like all the places we might look in debug info to find
> types when parsing expressions, or all the paths on the system we look at
> when trying to find SDK's or other system data, etc. In these sorts of
> composite operations, many of the sub-tasks can fail - and from the
> sub-tasks perspective the failure should be reported as an error. But only
> if all of them fail is the composite operation actually an error. In this
> sort of operation, you have to be careful not to lose any of the error
> messages along the way. For instance, when you look in a bunch of places
> for something, you should start from the most plausible place and proceed
> through more unlikely fallbacks. It's a common mistake in this situation
> to end up reporting as the error, the failure of the last operation. That
> is almost never helpful, it was the failure in the first couple (should
> really have succeeded) attempts that was actually meaningful.
>
> If we start handling errors in the "I'm returning a thing or an error"
> type interface you are considering, then accumulating many failures into
> one of these errors seems wrong.
>
> Instead, you could do this nicely by making a Status object that can
> accumulate (and by doing so check) the errors from its primitive
> operations. You'd also have to be able to mark the Status as Succeeding.
> Then the composite operation could turn the Status back into an Error, or
> pass it out as a Status, depending on its contract. So I think it is
> worthwhile keep two separate entities.
>
> Jim
>
>
>
>
> >
> > On Wed, May 10, 2017 at 6:09 PM Lang Hames <lhames at gmail.com> wrote:
> > Cool. This is just the rename portion, right?
> >
> > Sorry I didn't respond to your last message too.
> >
> > I suppose, but I'm not sure ErrorAnd captures the intended meaning very
> well. In any case, that's not super important at this stage since this
> isn't on the immediate horizon.
> >
> > Do you just mean that ErrorAnd isn't an especially nice name? I wasn't
> entirely sure what make_status<T>(...) was supposed to do so I assumed it
> was to create a pair of an Error and a T. If that's the case,
> make_with_error<T>(T, Error) (and WithError<T>) might be better names?
> >
> > Cheers,
> > Lang.
> >
> >
> > On Tue, May 9, 2017 at 8:58 PM, Zachary Turner <zturner at google.com>
> wrote:
> > I'm probably going to be looking at submitting this this week, more
> likely sooner rather than later. If I can get it all working hopefully
> even tomorrow.
> >
> > On Mon, May 1, 2017 at 5:49 PM Zachary Turner <zturner at google.com>
> wrote:
> > I suppose, but I'm not sure ErrorAnd captures the intended meaning very
> well. In any case, that's not super important at this stage since this
> isn't on the immediate horizon.
> >
> > On Mon, May 1, 2017 at 5:43 PM Lang Hames <lhames at gmail.com> wrote:
> > Hi Zachary,
> >
> > ... Then instead of Expected<T> you could have WithDiagnostics<T> that
> enforces the proper semantics.
> >
> > You mean something like an ErrorAnd<T>? Chris Bieneman floated that idea
> for some libObject code but we haven't got around to implementing it. If it
> were generically useful we could do something like that.
> >
> > Cheers,
> > Lang.
> >
> >
> > On Mon, May 1, 2017 at 5:36 PM, Zachary Turner <zturner at google.com>
> wrote:
> > Is there any chance of introducing something like make_status<T>() into
> llvm/Error.h, that constructs the llvm::Error in such a way that it still
> interoperates nicely with everything else? Then instead of Expected<T> you
> could have WithDiagnostics<T> that enforces the proper semantics.
> >
> > On Mon, May 1, 2017 at 5:33 PM Zachary Turner <zturner at google.com>
> wrote:
> > On Mon, May 1, 2017 at 5:27 PM Jim Ingham <jingham at apple.com> wrote:
> >
> > > On May 1, 2017, at 4:52 PM, Zachary Turner <zturner at google.com> wrote:
> > >
> > > Yea, grouping the error and the result together is one of the most
> compelling features of it. It's called Expected<T>, so where we would
> currently write something like:
> > >
> > > int getNumberOfSymbols(Error &Err) {}
> > >
> > > or
> > >
> > > Error getNumberOfSymbols(int &Count) {}
> > >
> > > You would now write:
> > >
> > > Expected<int> getNumberOfSymbols() {
> > > if (foo) return 1;
> > > else return make_error<DWARFError>("No symbols!");
> > > }
> > >
> > > and on the caller side you write:
> > >
> > > Error processAllSymbols() {
> > > if (auto Syms = getNumberOfSymbols()) {
> > > outs() << "There are " << *Syms << " symbols!";
> > > } else {
> > > return Syms.takeError();
> > > // alternatively, you could write:
> > > // consumeError(Syms.takeError());
> > > // return Error::success();
> > > }
> > > }
> > >
> >
> > Interesting.
> >
> > This pattern doesn't quite work for fetching symbols - maybe that really
> is more suitable as a Status than an Error. After all, number of symbols
> == 0 is not necessarily an error, there just might not have been any
> symbols (e.g. a fully stripped main); and I'm going to work on whatever
> symbols I get back, since there's nothing I can do about the ones that
> didn't make it. I just want to propagate the error so the user knows that
> there was a problem.
> >
> > Jim
> >
> > Sure, that was just a made up example. You could imagine that being
> some private function deep in the implementation details of a symbol
> parser, where you've got some header that's supposed to be N bytes, and
> getNumberOfSymbols() seeks to offset 42 and reads a 4 byte value and
> returns it, but the function sees that there's only 40 bytes in the header,
> so it's not that there's no symbols, it's that something is seriously
> messed up.
> >
> > In that case you could return an error such as this.
> >
> > Of course, the person who called this function can either propagate it,
> deal with it some other way and mask it, or whatever. Mostly I was just
> trying to show what the syntax looked like for grouping return values with
> errors.
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20170510/4aeafc6f/attachment-0001.html>
More information about the lldb-dev
mailing list