Unverified Commit d64332a0 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Improve the error message for non-normalized constraints (#127906)

We probably added RenderBox.layout after this error was created and of course we weren't writing tests back then...
parent 34f39a20
...@@ -2290,10 +2290,11 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im ...@@ -2290,10 +2290,11 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
informationCollector: () { informationCollector: () {
final List<String> stack = StackTrace.current.toString().split('\n'); final List<String> stack = StackTrace.current.toString().split('\n');
int? targetFrame; int? targetFrame;
final Pattern layoutFramePattern = RegExp(r'^#[0-9]+ +RenderObject.layout \('); final Pattern layoutFramePattern = RegExp(r'^#[0-9]+ +Render(?:Object|Box).layout \(');
for (int i = 0; i < stack.length; i += 1) { for (int i = 0; i < stack.length; i += 1) {
if (layoutFramePattern.matchAsPrefix(stack[i]) != null) { if (layoutFramePattern.matchAsPrefix(stack[i]) != null) {
targetFrame = i + 1; targetFrame = i + 1;
} else if (targetFrame != null) {
break; break;
} }
} }
...@@ -2301,7 +2302,6 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im ...@@ -2301,7 +2302,6 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
final Pattern targetFramePattern = RegExp(r'^#[0-9]+ +(.+)$'); final Pattern targetFramePattern = RegExp(r'^#[0-9]+ +(.+)$');
final Match? targetFrameMatch = targetFramePattern.matchAsPrefix(stack[targetFrame]); final Match? targetFrameMatch = targetFramePattern.matchAsPrefix(stack[targetFrame]);
final String? problemFunction = (targetFrameMatch != null && targetFrameMatch.groupCount > 0) ? targetFrameMatch.group(1) : stack[targetFrame].trim(); final String? problemFunction = (targetFrameMatch != null && targetFrameMatch.groupCount > 0) ? targetFrameMatch.group(1) : stack[targetFrame].trim();
// TODO(jacobr): this case is similar to displaying a single stack frame.
return <DiagnosticsNode>[ return <DiagnosticsNode>[
ErrorDescription( ErrorDescription(
"These invalid constraints were provided to $runtimeType's layout() " "These invalid constraints were provided to $runtimeType's layout() "
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// THIS TEST IS SENSITIVE TO LINE NUMBERS AT THE TOP OF THIS FILE
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
class RenderFoo extends RenderShiftedBox {
RenderFoo({ RenderBox? child }) : super(child);
@override
void performLayout() {
child?.layout(const BoxConstraints( // THIS MUST BE LINE 17
minWidth: 100.0, maxWidth: 50.0,
));
}
}
class Foo extends SingleChildRenderObjectWidget {
const Foo({ super.key, super.child });
@override
RenderFoo createRenderObject(BuildContext context) {
return RenderFoo();
}
}
// END OF SENSITIVE SECTION
void main() {
testWidgets('Stack parsing in non-normalized constraints error', (WidgetTester tester) async {
await tester.pumpWidget(const Foo(child: Placeholder()), Duration.zero, EnginePhase.layout);
final Object? exception = tester.takeException();
final String text = exception.toString();
expect(text, contains('BoxConstraints has non-normalized width constraints.'));
expect(text, contains('which probably computed the invalid constraints in question:\n RenderFoo.performLayout ('));
expect(text, contains('non_normalized_constraints_test.dart:17:12'));
}, skip: kIsWeb); // [intended] stack traces on web are insufficiently predictable
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment