I think he cheated and looked up the Church’s teachings.

When the Pope (1) intends to teach (2) by virtue of his supreme authority (3) on a matter of faith and morals (4) to the whole Church, he is preserved by the Holy Spirit from error.

In addition, he may in his ordinary teaching cite an infallible truth from the Church’s ordinary magisterium that is itself infallible. While this is infallible, it is not an exercise of papal infallibility, but the Holy Father citing an already infallible teaching.