diff --git a/examples/layers/rendering/src/sector_layout.dart b/examples/layers/rendering/src/sector_layout.dart index 069888c18f..3a9688c286 100644 --- a/examples/layers/rendering/src/sector_layout.dart +++ b/examples/layers/rendering/src/sector_layout.dart @@ -43,7 +43,7 @@ class SectorConstraints extends Constraints { bool get isNormalized => minDeltaRadius <= maxDeltaRadius && minDeltaTheta <= maxDeltaTheta; @override - bool get debugAssertIsNormalized { + bool debugAssertIsValid({ bool isAppliedConstraint: false }) { assert(isNormalized); return isNormalized; } diff --git a/packages/flutter/lib/src/rendering/block.dart b/packages/flutter/lib/src/rendering/block.dart index 8ad17f4a13..1b8515b6c7 100644 --- a/packages/flutter/lib/src/rendering/block.dart +++ b/packages/flutter/lib/src/rendering/block.dart @@ -171,7 +171,7 @@ class RenderBlock extends RenderBox @override double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicMainAxis(constraints, constraints.constrainWidth); @@ -186,7 +186,7 @@ class RenderBlock extends RenderBox @override double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicMainAxis(constraints, constraints.constrainWidth); @@ -201,7 +201,7 @@ class RenderBlock extends RenderBox @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicCrossAxis( @@ -216,7 +216,7 @@ class RenderBlock extends RenderBox @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicCrossAxis( diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 39ce2d6074..0a4a03eeb4 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -109,7 +109,7 @@ class BoxConstraints extends Constraints { /// Returns new box constraints that are smaller by the given edge dimensions. BoxConstraints deflate(EdgeInsets edges) { assert(edges != null); - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); double horizontal = edges.left + edges.right; double vertical = edges.top + edges.bottom; double deflatedMinWidth = math.max(0.0, minWidth - horizontal); @@ -124,7 +124,7 @@ class BoxConstraints extends Constraints { /// Returns new box constraints that remove the minimum width and height requirements. BoxConstraints loosen() { - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); return new BoxConstraints( minWidth: 0.0, maxWidth: maxWidth, @@ -175,14 +175,14 @@ class BoxConstraints extends Constraints { /// Returns the width that both satisfies the constraints and is as close as /// possible to the given width. double constrainWidth([double width = double.INFINITY]) { - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); return width.clamp(minWidth, maxWidth); } /// Returns the height that both satisfies the constraints and is as close as /// possible to the given height. double constrainHeight([double height = double.INFINITY]) { - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); return height.clamp(minHeight, maxHeight); } @@ -222,7 +222,7 @@ class BoxConstraints extends Constraints { /// Whether the given size satisfies the constraints. bool isSatisfiedBy(Size size) { - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); return (minWidth <= size.width) && (size.width <= maxWidth) && (minHeight <= size.height) && (size.height <= maxHeight); } @@ -273,8 +273,8 @@ class BoxConstraints extends Constraints { return b * t; if (b == null) return a * (1.0 - t); - assert(a.debugAssertIsNormalized); - assert(b.debugAssertIsNormalized); + assert(a.debugAssertIsValid()); + assert(b.debugAssertIsValid()); return new BoxConstraints( minWidth: ui.lerpDouble(a.minWidth, b.minWidth, t), maxWidth: ui.lerpDouble(a.maxWidth, b.maxWidth, t), @@ -303,7 +303,7 @@ class BoxConstraints extends Constraints { } @override - bool get debugAssertIsNormalized { + bool debugAssertIsValid({ bool isAppliedConstraint: false }) { assert(() { if (minWidth.isNaN || maxWidth.isNaN || minHeight.isNaN || maxHeight.isNaN) { List affectedFieldsList = []; @@ -340,7 +340,16 @@ class BoxConstraints extends Constraints { throw new FlutterError('BoxConstraints has non-normalized width constraints.\n$this'); if (maxHeight < minHeight) throw new FlutterError('BoxConstraints has non-normalized height constraints.\n$this'); - return isNormalized; + if (isAppliedConstraint) { + if (minWidth.isInfinite && minHeight.isInfinite) + throw new FlutterError('BoxConstraints requires infinite width and infinite height.\n$this'); + if (minWidth.isInfinite) + throw new FlutterError('BoxConstraints requires infinite width.\n$this'); + if (minHeight.isInfinite) + throw new FlutterError('BoxConstraints requires infinite height.\n$this'); + } + assert(isNormalized); + return true; }); return isNormalized; } @@ -356,13 +365,13 @@ class BoxConstraints extends Constraints { @override bool operator ==(dynamic other) { - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); if (identical(this, other)) return true; if (other is! BoxConstraints) return false; final BoxConstraints typedOther = other; - assert(typedOther.debugAssertIsNormalized); + assert(typedOther.debugAssertIsValid()); return minWidth == typedOther.minWidth && maxWidth == typedOther.maxWidth && minHeight == typedOther.minHeight && @@ -371,7 +380,7 @@ class BoxConstraints extends Constraints { @override int get hashCode { - assert(debugAssertIsNormalized); + assert(debugAssertIsValid()); return hashValues(minWidth, maxWidth, minHeight, maxHeight); } @@ -451,7 +460,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainWidth(0.0); } @@ -460,7 +469,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainWidth(0.0); } @@ -469,7 +478,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainHeight(0.0); } @@ -482,7 +491,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainHeight(0.0); } diff --git a/packages/flutter/lib/src/rendering/custom_layout.dart b/packages/flutter/lib/src/rendering/custom_layout.dart index 57b73c257c..365411fe6c 100644 --- a/packages/flutter/lib/src/rendering/custom_layout.dart +++ b/packages/flutter/lib/src/rendering/custom_layout.dart @@ -57,7 +57,7 @@ abstract class MultiChildLayoutDelegate { ); } try { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid(isAppliedConstraint: true)); } on AssertionError catch (exception) { throw new FlutterError( 'The $this custom multichild layout delegate provided invalid box constraints for the child with id "$childId".\n' @@ -232,7 +232,7 @@ class RenderCustomMultiChildLayoutBox extends RenderBox } Size _getSize(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrain(_delegate.getSize(constraints)); } diff --git a/packages/flutter/lib/src/rendering/editable_line.dart b/packages/flutter/lib/src/rendering/editable_line.dart index 013e7e9408..c987ca54c0 100644 --- a/packages/flutter/lib/src/rendering/editable_line.dart +++ b/packages/flutter/lib/src/rendering/editable_line.dart @@ -133,25 +133,25 @@ class RenderEditableLine extends RenderBox { @override double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainWidth(0.0); } @override double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainWidth(0.0); } @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainHeight(_preferredHeight); } @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return constraints.constrainHeight(_preferredHeight); } @@ -190,7 +190,7 @@ class RenderEditableLine extends RenderBox { // TODO(abarth): This logic should live in TextPainter and be shared with RenderParagraph. void _layoutText(BoxConstraints constraints) { assert(constraints != null); - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (_constraintsForCurrentLayout == constraints) return; // already cached this layout _textPainter.maxWidth = constraints.maxWidth; diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart index 518af43165..c9060fd43f 100644 --- a/packages/flutter/lib/src/rendering/flex.dart +++ b/packages/flutter/lib/src/rendering/flex.dart @@ -144,7 +144,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin { @override double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _delegate.getMinIntrinsicWidth(constraints, virtualChildCount); } @override double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _delegate.getMaxIntrinsicWidth(constraints, virtualChildCount); } @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _delegate.getMinIntrinsicHeight(constraints, virtualChildCount); } @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _delegate.getMaxIntrinsicHeight(constraints, virtualChildCount); } diff --git a/packages/flutter/lib/src/rendering/image.dart b/packages/flutter/lib/src/rendering/image.dart index 91e2ece48d..029a79b281 100644 --- a/packages/flutter/lib/src/rendering/image.dart +++ b/packages/flutter/lib/src/rendering/image.dart @@ -205,7 +205,7 @@ class RenderImage extends RenderBox { @override double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (_width == null && _height == null) return constraints.constrainWidth(0.0); return _sizeForConstraints(constraints).width; @@ -213,13 +213,13 @@ class RenderImage extends RenderBox { @override double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _sizeForConstraints(constraints).width; } @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (_width == null && _height == null) return constraints.constrainHeight(0.0); return _sizeForConstraints(constraints).height; @@ -227,7 +227,7 @@ class RenderImage extends RenderBox { @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _sizeForConstraints(constraints).height; } diff --git a/packages/flutter/lib/src/rendering/list.dart b/packages/flutter/lib/src/rendering/list.dart index abea020e2b..ded1f43931 100644 --- a/packages/flutter/lib/src/rendering/list.dart +++ b/packages/flutter/lib/src/rendering/list.dart @@ -82,7 +82,7 @@ class RenderList extends RenderVirtualViewport { } double _getIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); switch (mainAxis) { case Axis.vertical: return constraints.constrainWidth(0.0); @@ -102,7 +102,7 @@ class RenderList extends RenderVirtualViewport { } double _getIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); switch (mainAxis) { case Axis.vertical: return constraints.constrainHeight(_preferredExtent); diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index e4f2112abb..154426dcb8 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -382,9 +382,25 @@ abstract class Constraints { /// Whether the constraint is expressed in a consistent manner. bool get isNormalized; - /// Same as [isNormalized] but, in checked mode, throws an exception - /// if isNormalized is false. - bool get debugAssertIsNormalized; + /// Asserts that the constraints are valid. + /// + /// This might involve checks more detailed than [isNormalized]. + /// + /// For example, the [BoxConstraints] subclass verifies that the + /// constraints are not [NaN]. + /// + /// If the [isAppliedConstraint] argument is true, then even + /// stricter rules are enforced. This argument is set to true when + /// checking constraints that are about to be applied to a + /// [RenderObject] during layout, as opposed to constraints that may + /// be further affected by other constraints. For example, the + /// asserts for verifying the validity of + /// [RenderConstrainedBox.additionalConstraints] do not set this + /// argument, but the asserts for verifying the argument passed to + /// the [layout] method do. + /// + /// Returns the same as [isNormalized] if asserts are disabled. + bool debugAssertIsValid({ bool isAppliedConstraint: false }); } typedef void RenderObjectVisitor(RenderObject child); @@ -1136,7 +1152,7 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { /// work to update its layout information. void layout(Constraints constraints, { bool parentUsesSize: false }) { assert(constraints != null); - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid(isAppliedConstraint: true)); assert(!_debugDoingThisResize); assert(!_debugDoingThisLayout); final RenderObject parent = this.parent; diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index d6e730a597..821a7ceb2a 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -36,7 +36,7 @@ class RenderParagraph extends RenderBox { // TODO(abarth): This logic should live in TextPainter and be shared with RenderEditableLine. void _layoutText(BoxConstraints constraints) { assert(constraints != null); - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (_constraintsForCurrentLayout == constraints) return; // already cached this layout _textPainter.maxWidth = constraints.maxWidth; @@ -71,13 +71,13 @@ class RenderParagraph extends RenderBox { @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _getIntrinsicHeight(constraints); } @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return _getIntrinsicHeight(constraints); } diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index efb999868c..b42b80ff6f 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -37,7 +37,7 @@ class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin widths = computeColumnWidths(constraints); double rowTop = 0.0; diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index a260c528ea..08f8726f76 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -246,7 +246,7 @@ class RenderViewport extends RenderViewportBase with RenderObjectWithChildMixin< @override double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (child != null) return constraints.constrainWidth(child.getMinIntrinsicWidth(_getInnerConstraints(constraints))); return super.getMinIntrinsicWidth(constraints); @@ -254,7 +254,7 @@ class RenderViewport extends RenderViewportBase with RenderObjectWithChildMixin< @override double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (child != null) return constraints.constrainWidth(child.getMaxIntrinsicWidth(_getInnerConstraints(constraints))); return super.getMaxIntrinsicWidth(constraints); @@ -262,7 +262,7 @@ class RenderViewport extends RenderViewportBase with RenderObjectWithChildMixin< @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (child != null) return constraints.constrainHeight(child.getMinIntrinsicHeight(_getInnerConstraints(constraints))); return super.getMinIntrinsicHeight(constraints); @@ -270,7 +270,7 @@ class RenderViewport extends RenderViewportBase with RenderObjectWithChildMixin< @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); if (child != null) return constraints.constrainHeight(child.getMaxIntrinsicHeight(_getInnerConstraints(constraints))); return super.getMaxIntrinsicHeight(constraints); diff --git a/packages/flutter/lib/src/widgets/lazy_block.dart b/packages/flutter/lib/src/widgets/lazy_block.dart index f1425103b5..ffec2b3786 100644 --- a/packages/flutter/lib/src/widgets/lazy_block.dart +++ b/packages/flutter/lib/src/widgets/lazy_block.dart @@ -264,13 +264,13 @@ class _RenderLazyBlock extends RenderVirtualViewport<_LazyBlockParentData> { @override double getMinIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return getIntrinsicWidth(constraints); } @override double getMaxIntrinsicWidth(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return getIntrinsicWidth(constraints); } @@ -286,13 +286,13 @@ class _RenderLazyBlock extends RenderVirtualViewport<_LazyBlockParentData> { @override double getMinIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return getIntrinsicHeight(constraints); } @override double getMaxIntrinsicHeight(BoxConstraints constraints) { - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); return getIntrinsicHeight(constraints); } diff --git a/packages/flutter/test/rendering/constraints_test.dart b/packages/flutter/test/rendering/constraints_test.dart index 98d8d9b9b7..f8fb527a0d 100644 --- a/packages/flutter/test/rendering/constraints_test.dart +++ b/packages/flutter/test/rendering/constraints_test.dart @@ -37,7 +37,7 @@ void main() { result = 'no exception'; try { BoxConstraints constraints = new BoxConstraints(minWidth: double.NAN, maxWidth: double.NAN, minHeight: 2.0, maxHeight: double.NAN); - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); } on FlutterError catch (e) { result = '$e'; } @@ -49,7 +49,7 @@ void main() { result = 'no exception'; try { BoxConstraints constraints = new BoxConstraints(minHeight: double.NAN); - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); } on FlutterError catch (e) { result = '$e'; } @@ -61,7 +61,7 @@ void main() { result = 'no exception'; try { BoxConstraints constraints = new BoxConstraints(minHeight: double.NAN, maxWidth: 0.0/0.0); - assert(constraints.debugAssertIsNormalized); + assert(constraints.debugAssertIsValid()); } on FlutterError catch (e) { result = '$e'; }