Skip to content

Improve assertion messaging in RenderBox.size#128910

Closed
matthew-carroll wants to merge 7 commits into
masterfrom
matthew-carroll_renderbox-assertion-message-improvement
Closed

Improve assertion messaging in RenderBox.size#128910
matthew-carroll wants to merge 7 commits into
masterfrom
matthew-carroll_renderbox-assertion-message-improvement

Conversation

@matthew-carroll

Copy link
Copy Markdown
Contributor

I'm working on a group of custom RenderObjects and I keep hitting an assertion that's a bit ambiguous. I think it would be a good idea to add clarity to the associated assertion.

One outstanding question: What does !RenderObject.debugActiveLayout!.debugDoingThisLayoutWithCallback mean, and why do we care?

@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Jun 14, 2023

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @matthew-carroll thanks for looking into improving the error message. Can you add. test that checks both cases of this error?

assert(size._owner == this);
if (RenderObject.debugActiveLayout != null &&
!RenderObject.debugActiveLayout!.debugDoingThisLayoutWithCallback) {
assert(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably still be wrapped in an assertion.

"that child's layout().",
);
// The Flutter pipeline is currently running a layout pass.
// TODO: explain why we care that debugDoingThisLayoutWithCallback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked it back to #74402 for full context. Some widgets like RenderEditableTextState will access RenderBox.size when rebuilding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that property refers to a "rebuild"? Can you define that term for me? I've seen it used a few different ways.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot, I am not familiar with the specific change. The docs for debugDoingThisLayoutWithCallback say Whether invokeLayoutCallback for this render object is currently running. Certainly room to improve the docs there though. I'll file a bug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invokeLayoutCallback is quite a bit more diverse than just the concept of "rebuild", I believe.

invokeLayoutCallback may run by a RenderObject any number of times within that RenderObject's layout() behavior. Inside of that callback, you're permitted to mutate the element and render tree during layout, which is otherwise prohibited.

The double and triple negatives are throwing me, so I still can't tell the intention. It looks like if we're in a layout callback, we don't bother checking any conditions? We let the size access go through no matter what? This is the kind of stuff I was trying to improve. The intention of these conditions is really hard to figure out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #128913

// This RenderBox is not in the middle of running its own layout, which means some other
// object is trying to access the size.
if (RenderObject.debugActiveLayout != parent) {
throw AssertionError(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an AssertionError can we be even more helpful with a full FlutterError? You can add an ErrorHint for these helpful suggestions to resolve the issue.

@matthew-carroll

Copy link
Copy Markdown
Contributor Author

Can you add. test that checks both cases of this error?

Given that I'm not adding or removing any intentional condition, shouldn't there already be tests for this?

I was OK spending the time to write up different comments and error messages, but I purposefully only made non-behavior changes so that I wouldn't trigger the testing policy. If I'm still supposed to write tests, even for this change, then this is probably as far as I take the PR.

As for changing the type of thing being thrown - sure, I can make that whatever you want.

@christopherfujino
christopherfujino removed their request for review June 14, 2023 22:56
@Piinks

Piinks commented Jun 14, 2023

Copy link
Copy Markdown
Contributor

It looks like a lot of tests are currently failing, so if an existing test just needs an update, that would definitely check the box. Otherwise, per the bot we'll need Hixie to exempt tests for this. As a heads up, Hixie may ask if it's ok for this change to get accidentally reverted later, since tests would prevent something like that happening. 😉

@matthew-carroll

Copy link
Copy Markdown
Contributor Author

@Piinks do you happen to know the lint rules for composing strings and escaping single-quotes?

In my original commit, I escaped apostrophes because they conflicted with the string delimiters. But then the analysis said I used unnecessary escapes. So I removed the escapes. Now I've broken compilation because I have apostrophes that conflict with the string delimiters.

I'm not sure what the analyzer wants from me...

@Piinks

Piinks commented Jun 15, 2023

Copy link
Copy Markdown
Contributor

I think the analyzer expects single quotes, unless they are already part of the string, which in that case it prefers double quotes for the string so the escapes for single quotes are not necessary.

@flutter-dashboard

Copy link
Copy Markdown

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@goderbauer

Copy link
Copy Markdown
Member

(Triage): @matthew-carroll Do you still have plans to work on this and address the feedback given above?

@matthew-carroll

Copy link
Copy Markdown
Contributor Author

I'm giving up. It's proven far too difficult to interpret and resolve the test failures.

@jtmcdole
jtmcdole deleted the matthew-carroll_renderbox-assertion-message-improvement branch June 3, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants