rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.99k stars 882 forks source link

stress test from dartfmt #296

Closed nrc closed 9 years ago

nrc commented 9 years ago

This is so close to being parsable Rust code:

return doughnutFryer
    .start()
    .then((_) => _frostingGlazer.start())
    .then((_) => Future.wait([
          _conveyorBelts.start(),
          sprinkleSprinkler.start(),
          sauceDripper.start()
        ]))
    .catchError(cannotGetConveyorBeltRunning)
    .then((_) => tellEveryoneDonutsAreJustAboutDone())
    .then((_) => Future.wait([
          croissantFactory.start(),
          _giantBakingOvens.start(),
          butterbutterer.start()
        ])
            .catchError(_handleBakingFailures)
            .timeout(scriptLoadingTimeout, onTimeout: _handleBakingFailures)
            .catchError(cannotGetConveyorBeltRunning))
    .catchError(cannotGetConveyorBeltRunning)
    .then((_) {
  _logger.info("Let's eat!");
});

Can we do this well?

from http://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/

durka commented 9 years ago

Before:

fn main() {
    return doughnutFryer
        .start()
        .then(|_| _frostingGlazer.start())
        .then(|_| Future.wait([
              _conveyorBelts.start(),
              sprinkleSprinkler.start(),
              sauceDripper.start()
            ]))
        .catchError(cannotGetConveyorBeltRunning)
        .then(|_| tellEveryoneDonutsAreJustAboutDone())
        .then(|_| Future.wait([
              croissantFactory.start(),
              _giantBakingOvens.start(),
              butterbutterer.start()
            ])
                .catchError(_handleBakingFailures)
                .timeout(scriptLoadingTimeout, _handleBakingFailures)
                .catchError(cannotGetConveyorBeltRunning))
        .catchError(cannotGetConveyorBeltRunning)
        .then(|_| {
      _logger.info("Let's eat!");
    });
}

After:

fn main() {
    return doughnutFryer
        .start()
        .then(|_| _frostingGlazer.start())
        .then(|_| Future.wait([
              _conveyorBelts.start(),
              sprinkleSprinkler.start(),
              sauceDripper.start()
            ]))
        .catchError(cannotGetConveyorBeltRunning)
        .then(|_| tellEveryoneDonutsAreJustAboutDone())
        .then(|_| Future.wait([
              croissantFactory.start(),
              _giantBakingOvens.start(),
              butterbutterer.start()
            ])
                .catchError(_handleBakingFailures)
                .timeout(scriptLoadingTimeout, _handleBakingFailures)
                .catchError(cannotGetConveyorBeltRunning))
        .catchError(cannotGetConveyorBeltRunning)
        .then(|_| {
      _logger.info("Let's eat!");
    });
}

The stress would seem to be passed.

nrc commented 9 years ago

If we start with everything all on one line, what do we end up with?

Thanks for trying this out!

durka commented 9 years ago

Before:

fn main() { return doughnutFryer.start() .then(|_| _frostingGlazer.start()) .then(|_| Future.wait([ _conveyorBelts.start(), sprinkleSprinkler.start(), sauceDripper.start() ])) .catchError(cannotGetConveyorBeltRunning) .then(|_| tellEveryoneDonutsAreJustAboutDone()) .then(|_| Future.wait([ croissantFactory.start(), _giantBakingOvens.start(), butterbutterer.start() ]) .catchError(_handleBakingFailures) .timeout(scriptLoadingTimeout, _handleBakingFailures) .catchError(cannotGetConveyorBeltRunning)) .catchError(cannotGetConveyorBeltRunning) .then(|_| { _logger.info("Let's eat!"); }); }

Output:

Rustfmt failed at /Users/alex/Documents/research/nri/code/nri/fmt.rs:2: line exceeded maximum length (sorry)
nrc commented 9 years ago

Ah, that's not so good. I bet we're bailing some where and just reprinting the original.

This should be fun to look in to!

durka commented 9 years ago

Yeah, not sure where it bails in this one. There's no easy extern to point at!

marcusklaas commented 9 years ago

There's 3 things in here that we don't format yet:

After removing the return, and replacing the arrays by regular arguments, my branch produces this:

fn main() {
    doughnutFryer.start()
                 .then(|_| _frostingGlazer.start())
                 .then(|_| {
                     Future.wait(_conveyorBelts.start(),
                                 sprinkleSprinkler.start(),
                                 sauceDripper.start())
                 })
                 .catchError(cannotGetConveyorBeltRunning)
                 .then(|_| tellEveryoneDonutsAreJustAboutDone())
                 .then(|_| {
                     Future.wait(croissantFactory.start(),
                                 _giantBakingOvens.start(),
                                 butterbutterer.start())
                           .catchError(_handleBakingFailures)
                           .timeout(scriptLoadingTimeout, _handleBakingFailures)
                           .catchError(cannotGetConveyorBeltRunning)
                 })
                 .catchError(cannotGetConveyorBeltRunning)
                 .then(|_| {
                     _logger.info("Let's eat!");
                 });
}
durka commented 9 years ago

:+1:

durka commented 9 years ago

Er, method calls?

marcusklaas commented 9 years ago

Yea, the chainy kind a.b().c(). We don't do those yet. I'm working on it, though: https://github.com/nrc/rustfmt/pull/216

nrc commented 9 years ago

Yeah, I thought we did method calls, could be wrong though.

nrc commented 9 years ago

Oh right, makes sense.

nrc commented 9 years ago

Filed #302 and #303

killercup commented 9 years ago

This seems to work! (with rustfmt e80080deb60e78e7a57be4d693ca8e96cb14a1b2)

Before:

fn main() { return doughnutFryer.start() .then(|_| _frostingGlazer.start()) .then(|_| Future.wait([ _conveyorBelts.start(), sprinkleSprinkler.start(), sauceDripper.start() ])) .catchError(cannotGetConveyorBeltRunning) .then(|_| tellEveryoneDonutsAreJustAboutDone()) .then(|_| Future.wait([ croissantFactory.start(), _giantBakingOvens.start(), butterbutterer.start() ]) .catchError(_handleBakingFailures) .timeout(scriptLoadingTimeout, _handleBakingFailures) .catchError(cannotGetConveyorBeltRunning)) .catchError(cannotGetConveyorBeltRunning) .then(|_| { _logger.info("Let's eat!"); }); }

After:

fn main() {
    return doughnutFryer.start()
                        .then(|_| _frostingGlazer.start())
                        .then(|_| {
                            Future.wait([_conveyorBelts.start(),
                                         sprinkleSprinkler.start(),
                                         sauceDripper.start()])
                        })
                        .catchError(cannotGetConveyorBeltRunning)
                        .then(|_| tellEveryoneDonutsAreJustAboutDone())
                        .then(|_| {
                            Future.wait([croissantFactory.start(),
                                         _giantBakingOvens.start(),
                                         butterbutterer.start()])
                                  .catchError(_handleBakingFailures)
                                  .timeout(scriptLoadingTimeout, _handleBakingFailures)
                                  .catchError(cannotGetConveyorBeltRunning)
                        })
                        .catchError(cannotGetConveyorBeltRunning)
                        .then(|_| {
                            _logger.info("Let's eat!");
                        });
}
nrc commented 9 years ago

\o/ victory!